List in Java
In Java, the List interface is part of the java.util package and is used to hold an ordered collection of elements. It allows you to store and manipulate a sequence of elements. The List interface is implemented by various classes in Java, such as ArrayList, LinkedList, and Vector.
The List interface can store elements of primitive types as well as of reference types. It can also contain null and duplicate elements.
Working with Lists in Java
Working with lists in Java involves using various methods and operations to manipulate and retrieve data from the list. The following are some common tasks and operations you can perform with lists:
- Creating a List: To create a list object, you can choose using one of the implementations available in the java.util package, such as ArrayList, LinkedList, or Vector. For example:
- Adding Elements: Elements can be added to a list using the add() method. For example:
- Accessing Elements: Elements in the list can be accessed using the get() method, which retrieves the element at the specified index. For example:
- Removing Elements: Elements from the list can be removed using the remove() method. Elements can be removed by index or by specifying the element itself. For example:
- Updating Elements: Elements in the list can be updated by using the set() method that replaces the element at the specified index with a new value. For example:
- Checking List Size: The size of the list can be obtained by using the size() method, which returns the number of elements in the list. For example:
- Iterating over the List: To iterate over the elements in the list, you can use a loop, such as a for-each loop or a traditional for loop. For example:
List<String> myList = new ArrayList<>();
myList.add("Mango"); myList.add("Banana"); myList.add("Grape"); myList.add("Watermelon"); myList.add("Apple"); myList.add("Orange");
String fruit1 = myList.get(0); String fruit2 = myList.get(1); String fruit3 = myList.get(2); String fruit4 = myList.get(3);
myList.remove(2); // Removes the element at index 2 myList.remove("Banana");
myList.set(1, "Coconut"); //Replaces the element at index 1 with Coconut
int size = myList.size(); // Returns the size of the list
for (String fruit : myList) { // do your operation here } for (int i = 0; i < myList.size(); i++) { String fruit = myList.get(i); // do your operation here }
Here's an example of how to use the ArrayList class, which is one of the commonly used implementations of the List interface:
import java.util.ArrayList; import java.util.List; public class Sample { public static void main(String[] args) { // Create an ArrayList of strings List<String> names = new ArrayList<>(); // Adding elements to the list names.add("Alex"); names.add("Jay"); names.add("Jake"); names.add("Kelly"); names.add("Lina"); names.add("Ryan"); names.add("Kate"); // Accessing elements in the list System.out.println("First element: " + names.get(0)); System.out.println("Second element: " + names.get(1)); // Modifying an element in the list names.set(2, "Jenifer"); System.out.println("Updated third element: " + names.get(2)); // Removing an element from the list names.remove(0); System.out.println("List after removing first element: " + names); // Checking the size of the list System.out.println("Size of the list: " + names.size()); } }
The output of the above code is as follows:
First element: Alex Second element: Jay Updated third element: Jenifer List after removing first element: [Jay, Jenifer, Kelly, Lina, Ryan, Kate] Size of the list: 6
List Methods
Here are some commonly used methods of the 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 c) | This method appends all elements of the specified collection to the end of the list. |
boolean addAll(int index, Collection 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 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 | 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. |
This method returns an array containing all the elements in the list. |