In this guide, we will learn about List interface of the Java Collections framework with an example.
List Interface Overview
- List interface is a child interface of the Collection interface.
- If we want to represent a group of individual objects where duplicates are allowed and insertion order preserved then we should go for List.
- We can preserve insertion order and we can differentiate duplicate objects by using Index hence index will play a very important role in List.
- The user of this interface has precise control over where in the list each element is inserted.
- A list may contain multiple null elements.
- List interface can contain duplicate elements.
List interface implementations classes:
- ArrayList
- LinkedList
- Vector
- Stack
List Interface with It's ArrayList Implementation Class Example
Here is a simple List interface example using the ArrayList implementation class. This example demonstrates that List allows to store duplicate elements, null values, and maintains the insertion order:
package com.java.collections.interfaces;
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List < String > list = new ArrayList < > ();
// List allows you to add duplicate elements
list.add("element1");
list.add("element1");
list.add("element2");
list.add("element2");
System.out.println(list);
// List allows you to have ‘null’ elements.
list.add(null);
list.add(null);
System.out.println(list);
// insertion order
list.add("element1"); // 0
list.add("element2"); // 1
list.add("element4"); // 2
list.add("element3"); // 3
list.add("element5"); // 4
System.out.println(list);
// access elements from list
System.out.println(list.get(0));
System.out.println(list.get(4));
}
}
Output
[element1, element1, element2, element2]
[element1, element1, element2, element2, null, null]
[element1, element1, element2, element2, null, null, element1, element2, element4, element3, element5]
element1
null
List Interface with It's LinkedList Implementation Class Example
The following example shows how to create a LinkedList and add new elements to it.
package com.javaguides.collections.linkedlistexamples;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CreateLinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> fruits = new LinkedList<>();
// Adding new elements to the end of the LinkedList using add() method.
fruits.add("Banana");
fruits.add("Apple");
fruits.add("mango");
System.out.println("Initial LinkedList : " + fruits);
// Adding an element at the specified position in the LinkedList
fruits.add(2, "Watermelon");
System.out.println("After add(2, \"D\") : " + fruits);
// Adding an element at the beginning of the LinkedList
fruits.addFirst("Strawberry");
System.out.println("After addFirst(\"Strawberry\") : " + fruits);
// Adding an element at the end of the LinkedList
// (This method is equivalent to the add() method)
fruits.addLast("Orange");
System.out.println("After addLast(\"F\") : " + fruits);
// Adding all the elements from an existing collection to
// the end of the LinkedList
List<String> moreFruits = new ArrayList<>();
moreFruits.add("Grapes");
moreFruits.add("Pyrus");
fruits.addAll(moreFruits);
System.out.println("After addAll(moreFruits) : " + fruits);
}
}
Output:
Initial LinkedList : [Banana, Apple, mango]
After add(2, "D") : [Banana, Apple, Watermelon, mango]
After addFirst("Strawberry") : [Strawberry, Banana, Apple, Watermelon, mango]
After addLast("F") : [Strawberry, Banana, Apple, Watermelon, mango, Orange]
After addAll(moreFruits) : [Strawberry, Banana, Apple, Watermelon, mango, Orange, Grapes, Pyrus]
The List Interface Class Diagram
From the above diagram, the List interface extends the Collection interface. So, All methods of Collection interface are inherited to the List interface.
List Interface Methods
This class diagram shows a list of APIs/Methods that the List interface provides.
List Interface Common Implementation
List interface implementations classes:
- ArrayList
- LinkedList
- Vector
- Stack
What's next?
In this guide, we have learned all about the List interface, it's methods, and usage with examples.
In the next guide, we will learn about the ArrayList class of the Java collections framework.
Related Guides
- Collections Framework - The Collection Interface
- Collections Framework - The Set Interface
- Collections Framework - The SortedSet Interface
- Collections Framework - The List Interface
- Collections Framework - The Queue Interface
- Collections Framework - The Deque Interface
- Collections Framework - The Map Interface
- Collections Framework - The SortedMap Interface
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course