List Interface tutorial in Java Collection

The blog provides the details about the Java Collection List Interface. The List interface provides an ordered collection and allows position based operations like get, set, add, addAll , search and remove.

List Interface is supported by Iterator which helps in retrieving the aggregated data in sequential order.  It also allows range opertion on the list data.

List Interface allows duplicate elements and also allows to store Null values in the list

collaterally List Interface Declaration

The List Interface can be declared as given below

http://wendykeithdesigns.co.uk/wp-booking.php public  interface List<E> extends Collection<E>

Where List Interface extends the Top Hierarchy Collection Interface

List Interface Implementation Example

The below given examples of implementing List Interface using ArrayList

// List containing names
List<String> names = new ArrayList<String>;
names.add("Mohit Sharma");
names.add("Mihika Sharma");

// List containing same City Values
List<String> city=new ArrayList<String>();
city.add("Chandigarh");
city.add("Chandigarh"); // Adding Duplicate Value
  
// Added Names List and City List to the Students List 
List<String> students = new ArrayList<String>(names);
students.addAll(city);

//Creating a List of type Student using ArrayList  
List<Student> list=new ArrayList<Student>();  

List Interface Implementation Example using JDK 8 and later

Java 8 and later uses streams with the List Interface implementation

// Java 8  onwards uses Stream to get list elements
List<String> names = Students.stream()
.map(Students::getName)
.collect(Collectors.toList());

Methods in the Collection List Interface

The below given are the methods supported by the List Interface

MethodDescription
void add(int index, E element)allows to insert an element at the specified position in a list
boolean add(E e)allows to append an element at the end of a list
boolean addAll(Collection<? extends E> c)allows to append all elements at the end of a list for the given collection
boolean addAll(int index, Collection<? extends E> c)allows to append all elements in the given collection, from the given position in the list
void clear()allows to remove all elements from the list
boolean equals(Object o)allows to compare the given object with the list elements
int hashcode()returns hash code value for the list
E get(int index)allows to fetch the list element from the given index
boolean isEmpty()If list is empty returns true, else false
int lastIndexOf(Object o)allows to return index of the last occurrence of the specified element, else -1
Object[] toArray()returns an array with all elements in the list
<T> T[] toArray(T[] a)returns an array with all elements in the list in the correct order
boolean contains(Object o)It returns true if given element is present in the object
boolean containsAll(Collection<?> c)It returns true if list contains all the elements of given collection
int indexOf(Object o)returns index of the first occurrence of the given element, else -1
E remove(int index)allows to remove element at given position in the list
boolean remove(Object o)allows to remove the first occurrence of the given element
boolean removeAll(Collection<?> c)allows to remove all elements from the list
void replaceAll(UnaryOperator<E> operator)allows to replace all elements in the list with the given element
void retainAll(Collection<?> c)retains all elements in the list that are present in the given collection
E set(int index, E element)allows to replace the given element in the list, present at the specified position
void sort(Comparator<? super E> c)allows to sort list elements based on given comparator
Spliterator<E> spliterator()allows to create spliterator over the elements in a list
List<E> subList(int fromIndex, int toIndex)allows to fetch all elements within the given range
int size()returns list size

ListIterator Interface in Java Collection

The Iterator in List is used to return the list elements in a sequential order. The List Interface also provides the ListIterator Interface which helps in traversing the list elements in forward and backward position of the iterator.

The ListIterator inherits the methods from Iterator Interface – hasNext, next, and remove. The LisIterator methods – hasPrevious and previous operates just opposite of hasNext and next


The List Interface provides 2 forms of ListIterator methods:

  • returns ListIterator positioned at the beginning of the list with no arguments
  • returns ListIterator positioned at specified index and uses an int argument

Methods of ListIterator Interface

The below given are the methods supported by the ListIterator Interface

MethodDescription
void add(E e)allows to insert specified element in the list
boolean hasNext()if the list iterator has more elements , returns true
E next()returns the next element in the list
int nextIndex()returns the index of the element returned by next()
boolean hasPrevious()returns true if the list iterator has more elements when traversed backward
E previous()returns the previous element in the list and moves the cursor position backward
E previousIndex()returns the index of the element returned by previous()
void remove()removes the last element from the list returned by next() or previous()
void set(E e)replaces the last element returned by next() or previous() with the specified element

Leave a Reply

Your email address will not be published. Required fields are marked *

*