Java list: A Collection interface explained with 7 examples
The list interface in Java
The list is a sub-interface of Collection in Java. This is an ordered Collection that may contain duplicate elements.
The Collection in Java is the group of objects. The objects are known as elements that can be ordered and unordered. The collection has a few sub-interfaces and List is one of those. One other popular sub-interface is the Set. The set does not allow duplicate entries.
Important points about Java List
- The List inherits the operation from Collection.
- Additionally, the List interface includes positional access, search, iteration, and range-view operations.
- The elements in the List can be added, removed and searched.
- The index of list elements starts at 0.
- The List interface is implemented in different classes. The popular are ArrayList, LinkedList, Vector etc.
- Being an interface, the List object cannot be initialized.
See the following section for implementing the List interface in ArrayList, LinkedList and Vector classes. I will also show you a few examples of using the List methods for different operations.
An example of List Java with ArrayList class
This example shows how you may create a List object with ArrayList class. After creating an object, the elements in the List are added by using the add method of List. Finally, a for loop is used for displaying the elements in that List object:
See online demo and code
The code for creating and displaying the List:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<Integer> aList_demo = new ArrayList<Integer> (5); aList_demo.add(5); aList_demo.add(10); aList_demo.add(15); aList_demo.add(20); aList_demo.add(25); //Displaying List for (int i=0;i<aList_demo.size();i++){ System.out.println("The List element: " +aList_demo.get(i)); } } } |
An example of using LinkedList class
In this example, the Java LinkedList class is used with List interface for creating an object. Just like above example, the elements are added by using the add method. However, the list is declared as string type. So, it may contain only string elements.
See online demo and code
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<String> list_linkedList = new LinkedList<String>(); list_linkedList.add("The"); list_linkedList.add("Java"); list_linkedList.add("List"); list_linkedList.add("Tutorial"); System.out.println("The List with LinkedList class:"); System.out.print("\n"); System.out.print(list_linkedList); } } |
A demo of using the Vector class
The Vector class is among those classes that implement the List in Java. See this example where a list object is created with Vector class of string type. The same method i.e. add() is used for elements to be added in the list.
A for..each loop is used for displaying the list elements:
See online demo and code
The code for creating list with Vector class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<String> list_vector = new Vector<String>(); list_vector.add("The"); list_vector.add("List"); list_vector.add("with"); list_vector.add("Vector"); list_vector.add("Class"); System.out.println("The elements in list with Vector class:"); System.out.println("\n"); for (String currVectorElement : list_vector) { System.out.println(currVectorElement); } } } |
An example of removing element from the list
The remove() method can be used for deleting the specific element from the list. You may provide the index of element or object in the remove() method. If there are duplicate elements in the list, only first occurrence will be removed. If the element is found in the list, the method returns true otherwise false.
In this example, a list is created by using the ArrayList class. Five elements are added on the list by using the add method. After that, remove method is used for deleting 3rd index element. Have a look:
See online demo and code
The code for removing the list element by index:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<Integer> aList_demo = new ArrayList<Integer> (5); aList_demo.add(50); aList_demo.add(100); aList_demo.add(150); aList_demo.add(200); aList_demo.add(250); // System.out.println("List before using the remove method"); System.out.println(aList_demo); System.out.println("\n"); aList_demo.remove(2); System.out.println("List after using the remove method"); System.out.println(aList_demo); } } |
A demo of removing by object with duplicate entries
In this example, duplicate elements exist in the list. The remove method is used where an object is created with duplicate entries. See the output and code before and after using the remove method:
See online demo and code
The code of remove method by object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<String> aList_demo = new ArrayList<String> (5); aList_demo.add("Apple"); aList_demo.add("Mango"); aList_demo.add("Orange"); aList_demo.add("Mango"); aList_demo.add("Banana"); // System.out.println("The fruit list before remove method"); System.out.println(aList_demo); System.out.println("\n"); aList_demo.remove("Mango"); System.out.println("The fruit list after remove method"); System.out.println(aList_demo); } } |
You can see in the output graphic, the mango’s second occurrence still exist in the list after using the remove method.
Accessing list elements example
You may use the get method for accessing the specific elements of the list. In the first example of this tutorial, the get method is used in a for loop for displaying the complete list.
In the get() method, you may specify the index number that starts at 0. See this example where I created a list with four elements. The value of elements with index 1 and 3 displayed by using the get() method:
See online demo and code
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<String> aList_demo = new ArrayList<String> (4); aList_demo.add("Apple"); aList_demo.add("Mango"); aList_demo.add("Orange"); aList_demo.add("Banana"); System.out.println("The list element at 1 index is: " + aList_demo.get(1)); System.out.println("\n"); System.out.println("The list element at 3 index is: " + aList_demo.get(3)); } } |
Iterating through a list by using the iterator
As shown earlier, you may use a for loop for iterating through a list. Alternatively, you may use the list iterator for going through a list as shown in the example below:
See online demo and code
The code for iterating through a list with while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.*; public class dist_demo { public static void main(String []args) { List<String> aList_demo = new ArrayList<String>(); //Iterator<String> iterator = aList_demo.iterator(); aList_demo.add("Iterating"); aList_demo.add("Through"); aList_demo.add("a"); aList_demo.add("List"); Iterator<String> iterator = aList_demo.listIterator(); while (iterator.hasNext()) { System.out.println("The list item: "+iterator.next()); } } } |
You can see, the elements are displayed just like for loop.