Posts

Showing posts with the label arraylist

How to sort list of objects in java with examples

We could achieve list of objects sorting in java by using sort() method which is available under Collections class as well as by using Comparable and Comparator  interfaces. Comparable and Comparator are two interfaces in java used to sort list of objects. These interfaces are available under java.util package along with Collections  class and other classes and interfaces. Comparable interface contains the method of       public int compareTo(Object obj) Comparator interface contains the method of       public int compare(Object obj1, Object obj2) The compare() and compareTo() methods will return negative integer(-1), or zero(0) or positive integer(1). When the first obj1 is less than obj2 method will return negative integer. When obj1 is equals to obj2 will return zero and when obj1 is grater than obj2 it will return positive integer. If it's objects are stored in any Collection classes like ArrayList, HashSet in Array and that time we need to use compare(

List interface in java with examples

Read this article to know what is List interface and what are the List implemented classes in java with example programs.  List interface  represents a group of elements arranged like an array in java. List will grow dynamically when the elements are stored into it. List will allow duplicate elements to store into it. List interface implemented classes are,   ArrayList<E> Vector< E > LinkedList< E >           < E > represents the genetic type of storing element into List. List interface methods: The below are the list of methods which are available in List Interface. These methods are also available in subclasses of the List interface.   void add( int position, element obj): Adds the second argument into the List at the index specified by the first argument.   void add(element obj): This method appends the specified element to the end of the ArrayList. If the element is added successfully then the preceding meth