Java List Interface
In this tutorial, we will learn about the java.util.List interface and explanation of each method, guide lines of it along with the working examples of List.
java.util.List Interface
java.util.List
is an interface of collections framework in java, which is most widely used by the developers. List means a few items connected or grouped several items which are distinguished from others. Assume a custom class of student, students count will be N. Grouping these students can form a list and these students are interlinked and separated from others.
java.util.List
interface provides utility methods grouping similar objects and perform meaningful operation among grouped objects. List extends java.util.Collection
interface, which is a root interface for all collections and it represents an ordered collection of elements or objects, where each element can be accessed by its position or index.
An index refers to the position of an element in the list, starting from 0 to n where 0 for the first element and n for the last element in the list.
List Interface methods
Following are the most used methods from java.util.List
and description of each method.
Method | Description |
---|---|
boolean add(E e) | Adds the specified element to the end of the list. Returns true if the operation is successful otherwise false . |
void add(int index, E element) | Inserts element into the specified position, where index is position of element. |
boolean addAll (Collection<? extends E> c) | Appends all the elements in the specified collection to the end of the list, in the order that they are returned by its collection's iterator. Returns true if the operation is successful. |
boolean addAll (int index, Collection<? extends E> c) | Inserts all the elements in the specified collection into the list, starting at the specified position. |
void clear() | Removes all elements from list. |
boolean contains(Object o) | Checks list contains specified element, returns true if contains otherwise false . |
boolean containsAll (Collection<?> c) | Checks provided collection of elements all are present in the list, if yes returns true otherwise false . |
boolean equals(Object o) | Checks all the elements of the list to the given list elements are equal, if yes returns true otherwise false . |
E get(int index) | Element at the specified position in the list, will be returned. Index is a position of an elements. |
boolean isEmpty () | If the list contains no elements, then returns true otherwise false . |
Iterator<E> iterator() | Iterator will be returned for iterating over the elements. |
int lastIndexOf (Object o) | Specified element last index will be returned otherwise -1 |
ListIterator <E> listIterator () | List iterator will be returned with proper sequence of elements. |
ListIterator <E> listIterator (int index) | List iterator will be returned with proper sequence of elements from the specified index. |
E remove(int index) | Eleme at the specified position in the list will be removed. |
boolean remove(Object o) | First element which matches to the given element will be removed. |
boolean removeAll (Collection<?> c) | Elements of the list removed which matches to given list of elements. |
boolean retainAll (Collection<?> c) | Retains elements which are present in the given list |
E set(int index, E element) | Element at given position will be replaced with given element. |
int size() | Size of numbers of elements returns. |
List<E> subList (int fromIndex , int toIndex ) | Elements in the list between the given fromIndex(inclusive) and toIndex(exclusive) will be returned. |
Object[] toArray () | Returns elements Array in the order which they inserted. |
Guide Lines:
- At most priority to check in-built classes will not be sufficient to suffice requirement.
- Understand each method of
java.util.
List
interface and usage before implementing. - Choose appropriate data structure based on the requirement and performance factors.
- Handle all the scenarios like getting and replacing element at specified index and handle exceptions associated with it.
- Allow null values insertions, as list allows null elements.
- Make sure to implement
java.util.
Iterator
java.util.
ListIterator
- Make sure to cover all the implemented methods with test coverage 100%.
Use cases:
- Needs to group same type of elements , perform operations and computations.
- For user display purpose, like contacts from phone.
- Suitable for large computations over a memory.
- Data needs to organize in a structured manner.
List interface with custom class implementation example:
In this example, we will create a new class and which implements List interface.
import java.util.ArrayList;
import java.util.List;
public class MyList<E> extends ArrayList<E> implements List<E> {
// Override methods which needs to customized
}
Explanation:
From the above example, we created a custom MyList
class that implements the List<
E
>
interface and extends ArrayList
class and delegate all the method calls of MyList
instance to ArrayList
. By doing this, we can use functionality provided by the ArrayList
class and can be customize methods which are needed by overwriting those methods.
List interface with in-built class example:
In this example, we will use in-built class which is implemented the list interface.
import java.util.LinkedList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");//Adding fruits to the list
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");
System.out.println(fruits.toString());
//remove Apple from the fruits list
fruits.remove("Apple");
//After removing the Apple, fruit list
System.out.println(fruits.toString());
//get the index of Banana fruit
System.out.println("Index of Banana : "+fruits.indexOf("Banana"));
//Add papaya fruit at first position
fruits.add(0, "papaya");
System.out.println(fruits.toString());
}
}
Output:
[Apple, Banana, Orange, Mango]
[Banana, Orange, Mango]
Index of Banana : 0
[papaya, Banana, Orange, Mango]
Conclusion:
In this tutorial, we have covered the java.util.
List
interface, guidelines to consider before implementing it. We explored an example how to implement List by using existing methods and also an example of using with inbuilt class.