List Methods Java Tutorial
List interface is derived from Collection interface. The subclasses of List can utilize the methods Collection and List interfaces. The most used subclasses are ArrayList and LinkedList. ArrayList and LinkedList take the behavior of Collection interface and also the extra behavior given by List interface. List is an ordered collection of elements. List permits to insert the elements in the existing at any specified position. Methods exist to search the elements by their index numbers. The first element added gets by default 0 index.
List permits duplicate elements (Set does not allow). List adds (overrides) its own extra functionality (facilities) to the methods inherited from Collection like iterator(), remove(), add() hashCode() and equals().
List comes with an extra iterator ListIterator. ListIterator adds extra functionalities than offered by Iterator interface. ListIterator interface is derived from Iterator and adds extra methods that can be enjoyed by all the subclasses of List interface.
Following is the List interface signature
Following are the important List Methods Java
- Object get(int num): Returns the element (as object) existing at the specified index num. If the index num is given beyond the size of the List, the method throws IndexOutOfBoundsException.
- Object set(int num, Object obj): Used to replace the element. Replaces the existing element with new one obj at the specified index num. Returns the original element getting replaced.
- void add(int num, Object obj): Inserts the element obj at the mentioned index number num.
- int indexOf(Object obj): Returns index number of the element obj. If a number of elements by the same name obj exist, it returns the first occurrence. If no element by name obj exists, returns -1, the same style of String class.
- int lastIndexOf(Object obj): If multiple elements exist, it returns the last occurrence of obj. Returns -1 if no element by name obj exist.
- ListIterator listIterator(): returns a ListIterator object containing all the elements of List. ListIterator is more powerful (advantages) than Iterator.
- ListIterator listIterator(int index): Previous listIterator() method is overloaded. It returns all the elements of List from the specified index index (not all elements).
- List subList(int startIndex, int endIndex): Returns another List object that contains the elements from starting startIndex and ending with endIndex-1.
- Object remove(int num): Element at index number num is removed from List.
- contains(Object obj): Checks whether the element obj exists in the List or not. If exist, returns true else false.
Performance Tip: The methods of List, add(int num, Object obj) and remove(int index) are designed to be more efficient.
Note: Do no use equals() method to compare two lists as sometimes a list may have another list as an element. Try to avoid the usage of equals() and hashCode() methods with list classes as they are not well designed for list classes.
Four programs are given on List with different functionalities.
- List Methods: Uses frequently used methods like size(), add(), get(), contains(), indexOf(), lastIndexOf(), subList(), remove(), clear() and isEmpty().
- List Iteration: Comparing two lists with equals(), converting list elements to array with toArray(), printing array elements with foreach loop, printing list elements with Iterator (forward) and ListIterator (forward and backward).
- List Reversing Sorting: Reversing and sorting the list elements with methods asList(), sort(), reverseorder.
- List Unique Elements: Removing duplicate elements from list, sorting list elements, converting list to Set and again set to list.