List in Java with Examples
List in Java, is an interface that is generally used to hold an ordered collection of data.
The List interface can store elements of primitive types as well as of reference types. It can also contain null and duplicate elements.
The List interface provides index-based methods to insert, access, search, update, and remove the elements.
ArrayList, LinkedList, Stack, and Vector are built-in classes that implement the List interface. The most widely used List implementation classes are ArrayList and LinkedList.
To use the List interface, it should be imported from the java.util package.
List Methods
The following table shows methods of the Java List interface:
Method | Description |
---|---|
boolean add(E e) | This method is used to append the specified element to the end of the list. |
void add(int index, E element) | This method is used to insert the specified element at the specified position in the list. If there is already an element currently at that position then that element is shifted to the right. |
boolean addAll(Collection<? extends E> c) | This method appends all elements of the specified collection to the end of the list. |
boolean addAll(int index, Collection<? extends E> c) | This method inserts all elements of a specified collection at the specified position of the list. |
void clear() | This method removes all the elements from the list. It makes the list empty. |
boolean contains(Object o) | This method returns true if the list contains the specified element. |
boolean containsAll(Collection<?> c) | This method returns true if the list contains all the specified collection. |
boolean equals(Object o) | This method is used to check two lists for equality. This method returns true if the specified object is a list, both lists have the same size and all corresponding pairs of elements in the two comparing lists are equal. |
default void forEach(Consumer<? super T> action) | This method was added in Java 8. This method is used to perform the given action for each element of the list until all elements are processed otherwise an exception is thrown. |
E get(int index) | This method returns the element at the specified position in the list. |
int hashCode() | This method returns the hash code value of the list. |
int indexOf(Object o) | This method is used to get the index of the first occurrence of the specified element in this list. The method returns -1 if the specified element is not present in the list. |
boolean isEmpty() | This method is used to check if the list contains no elements. The method returns true If the list is empty else false is returned if the list is not empty. |
Iterator<E> iterator() | This method returns an iterator over the elements in the list. |
int lastIndexOf(Object o) | This method is used to get the index of the last occurrence of the specified element in this list. The method returns -1 if the specified element is not present in the list. |
boolean remove(Object o) | This method is used to remove the first occurrence of the specified element from this list. The list remains unchanged if it does not contain the specified element. |
E remove(int index) | This method is used to remove the element at the specified position in the list. This method also returns the element that is removed from the list. |
boolean removeAll(Collection<?> c) | This method removes all the elements from the list that are present in the specified collection. |
default boolean removeIf( Predicate<? super E> filter) | This method was added in Java 8. This method removes all the elements from the collection that satisfy the given predicate. |
boolean retainAll(Collection<?> c) | This method retains all the elements in the list that are contained in the specified collection. In other words, all the elements are removed from the list that are not present in the specified collection. |
E set(int index, E element) | This method replaces the element at the specified position with the specified element. |
int size() | The method returns the number of elements that is present in the list. |
default void sort(Comparator<? super E> c) | This method is used to sort the list to the order by implementing Comparator for order. |
List<E> subList(int fromIndex, int toIndex) | This method returns list of elements that are between the fromIndex position to toIndex position. |
Object[] toArray() | This method returns an array containing all the elements in the list. |
<T> T[] toArray(T[] a) | This method returns an array containing all the elements in the list. |
Create List in Java
The following code shows how to create a list object of specific type using ArrayList and LinkedList.
//Creating a List of String type using ArrayList
List<String> list = new ArrayList<>();
//Creating a List of Integer type using ArrayList
List<Integer> list = new ArrayList<>();
//Creating a List of Double type using ArrayList
List<Double> list = new ArrayList<>();
//Creating a List of User class type using ArrayList
List<User> list = new ArrayList<>();
//Creating a List of String type using LinkedList
List<String> list = new LinkedList<>();
//Creating a List of Integer type using LinkedList
List<Integer> list = new LinkedList<>();
//Creating a List of Double type using LinkedList
List<Double> list = new LinkedList<>();
//Creating a List of User class type using LinkedList
List<User> list = new LinkedList<>();
Add Items to a Java List Example
Here is a simple example to create a Java List using ArrayList implementation class to store elements of String type:
import java.util.ArrayList;
import java.util.List;
public class JavaListExample {
public static void main(String [] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("Android");
list.add("Spring Boot");
list.add("React");
System.out.println(list);
}
}
Output:
Access Items of a Java List Example
Here is an example to access items of a Java List:
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String [] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("Android");
list.add("Spring Boot");
list.add("React");
//Access list items
for(int index = 0; index < list.size(); index++) {
System.out.println(list.get(index));
}
}
}
Output:
Python
Android
Spring Boot
React
Sort Items in a Java List Example
Here is an example to sort items in a Java List:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> animalList = new ArrayList<>();
animalList.add("Zebra");
animalList.add("Lion");
animalList.add("Monkey");
animalList.add("Tiger");
animalList.add("Crocodile");
animalList.add("Ape");
List<Integer> numberList = new ArrayList<>();
numberList.add(7);
numberList.add(4);
numberList.add(1);
numberList.add(6);
numberList.add(3);
numberList.add(2);
numberList.add(5);
System.out.println("Sorting items in ascending order");
Collections.sort(animalList);
Collections.sort(numberList);
System.out.println(animalList);
System.out.println(numberList);
System.out.println("Sorting items in descending order");
Collections.sort(animalList, Collections.reverseOrder());
Collections.sort(numberList, Collections.reverseOrder());
System.out.println(animalList);
System.out.println(numberList);
}
}
Output:
[Ape, Crocodile, Lion, Monkey, Tiger, Zebra]
[1, 2, 3, 4, 5, 6, 7]
Sorting items in descending order
[Zebra, Tiger, Monkey, Lion, Crocodile, Ape]
[7, 6, 5, 4, 3, 2, 1]
Get Total Items in a Java List Example
Here is an example to get the number items in a Java List:
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> animalList = new ArrayList<>();
animalList.add("Zebra");
animalList.add("Lion");
animalList.add("Monkey");
animalList.add("Tiger");
animalList.add("Crocodile");
animalList.add("Ape");
//using size() method to get the size of list
int listSize = animalList.size();
System.out.println("The size of elements in the list is " + listSize);
}
}
Output:
ListIterator Interface Example
The ListIterator interface is used to traverse the list in the forward or backward direction. Here is an example to traverse the list of items in the forward and backward direction:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListExample {
public static void main(String[] args) {
List<String> animalList = new ArrayList<>();
animalList.add("Zebra");
animalList.add("Lion");
animalList.add("Monkey");
animalList.add("Tiger");
animalList.add("Crocodile");
animalList.add("Ape");
//ListIterator interface
ListIterator<String> iterator = animalList.listIterator();
System.out.println("Traversing the list in the forward direction");
while (iterator.hasNext()) {
System.out.println("Iterator value = " + iterator.next());
}
System.out.println("Traversing the list in the backwards direction");
while (iterator.hasPrevious()) {
System.out.println("Iterator value = " + iterator.previous());
}
}
}
Output:
Iterator value = Zebra
Iterator value = Lion
Iterator value = Monkey
Iterator value = Tiger
Iterator value = Crocodile
Iterator value = Ape
Traversing the list in the backwards direction
Iterator value = Ape
Iterator value = Crocodile
Iterator value = Tiger
Iterator value = Monkey
Iterator value = Lion
Iterator value = Zebra