A Complete Java List Tutorial

Java List Tutorial

 
A list is a non-linear collection of data where then data stored can be of a different type, and also, the data is stored in a non-contiguous memory location.
 

Features of Java List

  1. Elements can be inserted. accessed, iterated and removed according to the order in which they appear internally in Java List.
  2. Each element in a Java List has an index.
  3. The 1st elements have the index 0, and last has the index n-1, where n is the size of the Java List
  4. Java List interface is a standard Java Interface, and it is a subtype of the Java Collection interface, means List inherits from Collection
  5. It is possible to have different kinds of elements in Java List
  6. A Java List may contain duplicate elements
  7. Since List is an interface hence it does not have any constructors

Java List vs Java Set

  1. Java List and Java Set both are interfaces and also they both represent 
  2. Java List can have duplicate elements whereas Java Sets can only have unique elements, in sets if we try to insert duplicate element it will replace the existing.
  3. Java List elements are stored in the order in which they are inserted whereas Java Sets does not store the elements in the order in which they are inserted
  4. Java List permits any number of "Null" values whereas java sets can only have one "Null" value
  5. Java List can be traversed in both forward and backward direction unlike Java Sets which can only be traversed in the forward direction
Java List is an interface, so we need to instantiate a concrete implementation in order to use it. We can create a List using any of the following:
  1. java.util.ArrayList
  2. java.util.LinkedList
  3. java.util.Vector
  4. java.util.Stack
  1. import java.util.ArrayList;  
  2. import java.util.LinkedList;  
  3. import java.util.List;  
  4. import java.util.Stack;  
  5. import java.util.Vector;  
  6.             
  7. class Csharpcorner            
  8. {            
  9.     public static void main(String[] args)        
  10.     {            
  11.         List list1 = new ArrayList();    
  12.         List list2 = new LinkedList();  
  13.         List list3 = new Vector();  
  14.         List list4 = new Stack();  
  15.          
  16.    }            
  17. }   
The above code demonstrates the creation of Java List using Java ArrayList, Java LinkedList, Java Vector and Java Stack
 

Java List Methods

 

1. List.add()

 
Syntax
 
1. boolean add(E e)
 
It appends the specified element to the end of this list
 
2. void add(int index, E element)
 
It is used to insert the specified element at the specified position in a list 
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.         list1.add("Maruti");  
  10.         list1.add("Benz");  
  11.         list1.add(1"Audi");  
  12.    }            
  13. }  
The above code, demonstrates both of the versions of add method
 

2. List.addAll()

 
Syntax
 
1. boolean addAll(Collection<? extends E>c)
 
It appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collections' iterator 
 
2. boolean addAll(int index, Collection<? extends E>c)
 
It inserts all of the elements in the specified collection into this list at the specified position.
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.           
  10.         List list2 = new ArrayList();  
  11.           
  12.         list1.add("Maruti");  
  13.         list1.add("Benz");  
  14.         list1.add(1"Audi");  
  15.           
  16.         list2.add("Wagonar");  
  17.         list2.add("Lambogini");  
  18.           
  19.         list1.addAll(list2);  
  20.           
  21.         list1.addAll(3, list2);  
  22.    }            
  23. }                     
The above code, demonstrates both of the versions of addAll method
 

3. List.clear()

 
Syntax
 
void clear()
 
It removes all of the elements from this list
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         list1.clear();  
  15.    }            
  16. }  
In the above code, we are removing all the elements from the List list1
 

4. List.contains()

 
Syntax
 
boolean contains(Object o)
 
It returns true if this list contains the specified element
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         list1.contains("Audi");  
  15.    }            
  16. }    
In the above code, we are checking whether "Audi" exists in List list1 or not
 

5. List.containsAll()

 
Syntax 
 
boolean containsAll(Collection<?> c)
 
It returns true if this list contains all of the elements of the specified collection.
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.           
  10.         List list2 = new ArrayList();  
  11.           
  12.         list1.add("Maruti");  
  13.         list1.add("Benz");  
  14.         list1.add(1"Audi");  
  15.           
  16.         list2.add("Wagonar");  
  17.         list2.add("Lambogini");  
  18.           
  19.         list1.containsAll(list2);  
  20.    }            
  21. }               
In the above code, we are checking whether all elements of List list2 exists in List list1 or not
 

6. List.equals()

 
Syntax
 
boolean equals(Object o)
 
It compares the specified Object with this list for equality
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.           
  10.         List list2 = new ArrayList();  
  11.           
  12.         list1.add("Maruti");  
  13.         list1.add("Benz");  
  14.         list1.add(1"Audi");  
  15.           
  16.         list2.add("Wagonar");  
  17.         list2.add("Lambogini");  
  18.           
  19.         list1.equals(list2);  
  20.    }            
  21. }          
The above code checks if List list1 is equal to List list2
 

7. List.get()

 
Syntax 
 
E get(int index)
 
It returns the element at the specified position in this list
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.         list1.add("Maruti");  
  10.         list1.add("Benz");  
  11.         list1.add(1"Audi");  
  12.           
  13.         list1.get(1);  
  14.    }            
  15. }   
In the above code, we are fetching the 2nd element for the List list1
 

8. List.hashCode() 

 
Syntax
 
int hashCode()
 
It returns the hash code value for this list
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();  
  9.         list1.add("Maruti");  
  10.         list1.add("Benz");  
  11.         list1.add(1"Audi");  
  12.           
  13.         list1.hashCode();  
  14.    }            
  15. }     
The above code witll return 386995649 as output.
 

9. List.indexOf()

 
Syntax
 
indexOf(Object o)
 
It returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         list1.indexOf("Audi");  
  15.    }            
  16. }  
The above code, demostrates the use of indexOf method
 

10. List.isEmpty()

 
Syntax
 
boolean isEmpty()
 
It returns true if this list contains no elements.
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         list1.isEmpty();  
  15.    }            
  16. }   
In the above code, we are checking if the list is empty or not
 

11. List.iterator()

 
Syntax
 
Iterator<E> iterator()
 
It iterators over the elements in this list in a proper sequence.
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         Iterator iterator = list1.iterator();  
  15.         while(iterator.hasNext())  
  16.         {  
  17.             out.println(iterator.next());  
  18.         }       
  19.    }            
  20. }     
In the above code, we are creating an Iterator named iterator which is used to iterate over the List list1
 

12. List.lastIndexOf()

 
Syntax
 
int lastIndexOf(Object o)
 
It returns the index of the last occurrence of the specified element in this list or -1 if this list does not contain the element.
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         int ch = list1.lastIndexOf("Audi");  
  15.         out.print(ch);  
  16.    }            
  17. }    
In the above code, we wil get output as 1
 

13. List.listIterator()

 
Syntax
 
1. ListIterator listIterator()
 
It returns a list iterator over the elements in this list
 
2.  ListIterator listIterator(int index)
 
It returns a list iterator over the elements in this list, starting at the specified position in the list.
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         Iterator iterator = list1.listIterator();  
  15.         while(iterator.hasNext())    
  16.         {    
  17.             out.println(iterator.next());    
  18.         }   
  19.           
  20.         Iterator iterator1 = list1.listIterator(1);  
  21.         while(iterator1.hasNext())    
  22.         {    
  23.             out.println(iterator1.next());    
  24.         }   
  25.    }            
  26. }       
The above code demonstrates both the versions of listIterator method. The first version will print all the 3 elements whereas the second version will print all elements except the 1st one.
 

14. List.remove()

 
Syntax
 
1. E remove(int index)
 
It removes the elements at the specified position in this list.
 
2. boolean remove(Object o)
 
It removes the first occurrence of the specified element from this list if it is present
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         list1.remove(1);  
  15.         out.println(list1);  
  16.           
  17.         list1.remove("Benz");  
  18.         out.println(list1);  
  19.    }            
  20. }       
In the above code, we are first removing "Audi" and then "Benz", so we will be left with "Maruti"
 

15. List.removeAll()

 
Syntax
 
boolean removeAll(Collection<?> c)
 
It removes from this list all of its elements that are contained in the specified collection.
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3.             
  4. class Csharpcorner            
  5. {            
  6.     public static void main(String[] args)        
  7.     {            
  8.         List list1 = new ArrayList();    
  9.          
  10.         list1.add("Maruti");  
  11.         list1.add("Benz");  
  12.         list1.add(1"Audi");  
  13.           
  14.         List list2 = new ArrayList();    
  15.          
  16.         list2.add("Maruti");  
  17.         list1.add("Benz");  
  18.           
  19.         list1.removeAll(list2);  
  20.    }            
  21. }              
The above code will remove all the elements of list2 from list1
 

16. List.replaceAll()

 
Syntax 
 
default void replaceAll(UnaryOperator<E> operator)
 
It replaces each element of this list with the result of applying the operator to that element
  1. import static java.lang.System.out;  
  2. import java.util.*;  
  3. import java.util.function.UnaryOperator;  
  4.   
  5. class MyOperator implements UnaryOperator<String>  
  6. {  
  7.     @Override  
  8.     public String apply(String t) {  
  9.         return t.toLowerCase();  
  10.     }  
  11. }  
  12.             
  13. class Csharpcorner            
  14. {            
  15.     public static void main(String[] args)        
  16.     {            
  17.         List list1 = new ArrayList();    
  18.          
  19.         list1.add("Maruti");  
  20.         list1.add("Benz");  
  21.         list1.add(1"Audi");  
  22.           
  23.         list1.replaceAll(new MyOperator());  
  24.    }            
  25. }              
The above code demonstrates the use of replaceAll methid where we are changing the case of all elements
 

17. List.retainAll()

 
Syntax
 
boolean retainAll(Collection<?> c)
 
It retains only the elements in this list that are contained in the specified collection.
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");    
  13.             
  14.         List list2 = new ArrayList();      
  15.            
  16.         list2.add("Maruti");    
  17.         list1.add("Benz");    
  18.             
  19.         list1.retainAll(list2);    
  20.         out.print(list1);  
  21.    }              
  22. }                       
In the above code, we are performing the intersection operation, i.e keeping the elements that common to both the lists
 

18. List.set()

 
Syntax
 
E set(int index, E element)
 
It replaces the elements at the specified position in this list with the specified element
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");    
  13.             
  14.         list1.set(1"Alto");  
  15.           
  16.         out.print(list1);  
  17.    }              
  18. }      
The above code replaces "Audi" with "Alto".
 

19. List.size() 

 
Syntax
  
int size()
 
It returns the number of elements in the list
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");    
  13.           
  14.         out.print(list1.size());  
  15.    }              
  16. }     
The above code prints the size of the List.
 

20. List.sort()

 
Syntax
 
void sort(Comparator<? super E> c)
 
It sorts this list according to the order induced by the specified Comparator.
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");   
  13.           
  14.         Comparator<String> com = Collections.reverseOrder();  
  15.           
  16.         list1.sort(com);  
  17.         out.print(list1);  
  18.    }              
  19. }         
In the above code, we will be printing the list in revese order of the ASCII.
 

21. List.spliterator()

 
Syntax
 
Spliterator spliterator()
 
It creates a Spliterator over the elements in this list
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");   
  13.           
  14.         Spliterator<String> spl = list1.spliterator();  
  15.         out.print(spl);  
  16.    }              
  17. }     
The above code will print the Spliterator version of the given List.
 

22. List.subList()

 
Syntax
 
List<E> subList(int frontIndex, int toIndex)
 
It returns a view of the portion of this list between the specified frontIndex, inclusive, and toIndex, exclusive
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");   
  13.           
  14.         List sub = list1.subList(1, 3);  
  15.         out.print(sub);  
  16.    }              
  17. }      
The above code will generate a List containg list1[1] and list1[2]
 

23. List.toArray()

 
Syntax
 
1. Object[] toArray[]
 
It returns an array containing all of the elements in this list in a proper sequence.
  
2. <T> T[] toArray(T[] a)
 
It returns an array containing all of the elements in this list in a proper sequence; the runtime type of the returned array is that of the specified array 
  1. import static java.lang.System.out;    
  2. import java.util.*;    
  3.               
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         List list1 = new ArrayList();      
  9.            
  10.         list1.add("Maruti");    
  11.         list1.add("Benz");    
  12.         list1.add(1"Audi");     
  13.           
  14.         Object[] a1 = list1.toArray();  
  15.         out.print(a1);  
  16.    }              
  17. }   
In the above code, we are converting java.util.List to java.lang.reflect.Array.
 

Conclusion 

 
In this article, we learned about Java List, features of Java List, the difference between Java List and Java Set, Java List Methods and Java Implementation of Java List and Java List method


Similar Articles