The perfect place for easy learning...

Java Programming

×

Topics List


Java List Interface





The List interface is a child interface of the Collection interface. The List interface is available inside the java.util package. It defines the methods that are commonly used by classes like ArrayList, LinkedList, Vector, and Stack.

🔔 The List interface extends Collection interface.

🔔 The List interface allows duplicate elements.

🔔 The List interface preserves the order of insertion.

🔔 The List allows to access the elements based on the index value that starts with zero.

The List interface defines the following methods.

List interface in java

Let's consider an example program on ArrayList to illustrate the methods of List interface.

Example
import java.util.ArrayList;
import java.util.List;

public class ListInterfaceExample {
	
	public static void main(String[] args) {

		List list_1 = new ArrayList();
		List<String> list_2 = new ArrayList<String>();
		
		list_1.add(0, 10);
		list_1.add(1, 20);
		list_2.add(0, "BTech");
		list_2.add(1, "Smart");
		list_2.add(2, "Class");
		
		list_1.addAll(1, list_2);		
		System.out.println("\nElements of list_1: " + list_1);
		
		System.out.println("\nElement at index 3: " + list_1.get(3));		
		
		System.out.println("\nSublist : " + list_1.subList(2, 5));	
		
		list_1.set(2, 10);		
		System.out.println("\nAfter updating the value at index 2: " + list_1);	
		
		System.out.println("\nIndex of value 10: " + list_1.indexOf(10));
		System.out.println("\nLast index of value 10: " + list_1.lastIndexOf(10));
	}
}

When we run this code, it produce the following output.

List interface in java