Comparison of List vs LinkedList in Java
In Java, List is an interface in java.util
package whereas LinkedList is a class in the java.util
package. Both of this data structure is used to store the ordered collection of an elements of same type. The advantage of this over an array is there is no limitations on the number of elements it can hold. As and when needed we can dynamically add the elements.
The List is an interface so it can be implemented by a ArrayList, Vector, Stack, LinkedList class in Java. Hence, while working on real time application we need to know some difference between List vs LinkedList so as to make an appropriate selection of data structure.
Here, the table below lists some of the key differences between the interface List vs LinkedList class.
List | Linked List |
---|---|
The List is a child interface of Collection interface. | The Linked list is a class of java.util package that implements the List interface. |
It provides the way to store ordered collection. | This class is an implementation of Linked List data structure. |
We need a class that implements this List in order to create an object. | We can create object dynamically for linked list class. |
We can access elements by their integer index and search for elements in the list. | As this is doubly linked list implementation, either we can travel from the beginning or end. |
Some implementations prohibit null elements | It allows null elements in the linked list. |
Insertion is done using add() and addAll() methods. |
Insertion is done using add() , addAll() , push() , addFirst() , addLast() methods. |
Deletion is done using remove() and removeAll() methods. |
Deletion is done using remove() , pop() , removeFirst() , removeFirstOccurence() , removeLast() , removeLastOccurence methods. |
Examples demonstrating List vs LinkedList
Example 1 : Operations on List implemented by ArrayList
In this example, we are performing some operations like add elements, add another list, remove elements, update the value, checking for the presence of elements and getting the size of the list.
// Program to demonstrate list vs LinkedList
import java.util.*;
public class Main {
public static void main(String[] args) {
// Initializing
List l1 = new ArrayList < > ();
List l2 = new ArrayList < > (Arrays.asList(30, 40, 50));
// Adding the elements to list 1
l1.add(10);
l1.add(20);
l1.add(null);
System.out.println("List is" + l1);
// appending list 2 to list 1
l1.addAll(l2);
System.out.println("Now, List is" + l1);
// Checking for the list elements
System.out.println("Does list contains 45" + l1.contains(45));
System.out.println("Does list contains 40" + l1.contains(40));
// Removing the element with value
l1.remove(null);
System.out.println("After removal List is" + l1);
// Removing the element with Index
l1.remove(2);
System.out.println("After removing value from index 2. List is " + l1);
// Update the element at index 2
l1.set(2, 45);
System.out.println("After updating the value at index 2. List is " + l1);
// Size of List
System.out.println("Size of list is " + l1.size());
}
}
Output
List is[10, 20, null]
Now, List is[10, 20, null, 30, 40, 50]
Does list contains 45false
Does list contains 40true
After removal List is[10, 20, 30, 40, 50]
After removing value from index 2. List is [10, 20, 40, 50]
After updating the value at index 2. List is [10, 20, 45, 50]
Size of list is 4
Example 2 : Operations on Linked List
In this example, we are performing some operations like add elements, add another list, using push, pop methods, remove elements, update the value, checking for the presence of elements and getting the size of the linked list.
// Program to demonstrate list vs LinkedList
import java.util.*;
public class Main {
public static void main(String[] args) {
// Initializing
LinkedList l1 = new LinkedList < > ();
LinkedList l2 = new LinkedList < > (Arrays.asList(30, 40, 50));
// Adding the elements to list 1
l1.add(10);
l1.add(20);
l1.add(null);
// Adding elements at beginning and end of list
l1.addFirst(5);
l1.addLast(25);
System.out.println("Linked list is " + l1);
// appending list 2 to list 1
l1.addAll(l2);
System.out.println("Now, Linked list is " + l1);
// Checking for the list elements
System.out.println("Does list contains 45 " + l1.contains(45));
System.out.println("Does list contains 40 " + l1.contains(40));
// Removing the element with value
l1.remove(null);
System.out.println("After removal Linked list is " + l1);
// Removing the element with Index
l1.remove(2);
System.out.println("After removing value from index 2. Linked list is " + l1);
// Performs push and pop operation considering linked list as a StackOverflowError
l1.push(80);
System.out.println("After pushing 80 Linked list is " + l1);
System.out.println("Element popped out is " + l1.pop());
// Update the element at index 2
l1.set(2, 45);
System.out.println("After updating the value at index 2. Linked list is " + l1);
// Size of List
System.out.println("Size of Linked list is " + l1.size());
}
}
Output
Linked list is [5, 10, 20, null, 25]
Now, Linked list is [5, 10, 20, null, 25, 30, 40, 50]
Does list contains 45 false
Does list contains 40 true
After removal Linked list is [5, 10, 20, 25, 30, 40, 50]
After removing value from index 2. Linked list is [5, 10, 25, 30, 40, 50]
After pushing 80 Linked list is [80, 5, 10, 25, 30, 40, 50]
Element popped out is 80
After updating the value at index 2. Linked list is [5, 10, 45, 30, 40, 50]
Size of Linked list is 6
Example 3 : Operations on List implemented by Stack
In this example, we are performing some operations like add elements, add another list, remove elements, update the value, checking for the presence of elements and getting the size of the list.
// Program to demonstrate list vs LinkedList
import java.util.*;
public class Main {
public static void main(String[] args) {
// Initializing
List l1 = new Stack();
List l2 = new Stack();
// Adding the elements to list 2
l2.add(30);
l2.add(40);
l2.add(50);
// Adding the elements to list 1
l1.add(10);
l1.add(20);
l1.add(null);
System.out.println("List is" + l1);
// appending list 2 to list 1
l1.addAll(l2);
System.out.println("Now, List is" + l1);
// Checking for the list elements
System.out.println("Does list contains 45" + l1.contains(45));
System.out.println("Does list contains 40" + l1.contains(40));
// Removing the element with value
l1.remove(null);
System.out.println("After removal List is" + l1);
// Removing the element with Index
l1.remove(2);
System.out.println("After removing value from index 2. List is " + l1);
// Update the element at index 2
l1.set(2, 45);
System.out.println("After updating the value at index 2. List is " + l1);
// Size of List
System.out.println("Size of list is " + l1.size());
}
}
Output
List is[10, 20, null]
Now, List is[10, 20, null, 30, 40, 50]
Does list contains 45false
Does list contains 40true
After removal List is[10, 20, 30, 40, 50]
After removing value from index 2. List is [10, 20, 40, 50]
After updating the value at index 2. List is [10, 20, 45, 50]
Size of list is 4
Summary
The knowledge of List vs LinkedList is very useful while working on real time applications in Java. In many situations, we will need to maintain the ordered collection of elements where we need to make a selection between List vs LinkedList class. So, depending upon the operations required and size of data we can make an appropriate selection among this two. In this tutorial, we covered some important methods used with a list and linked list along with the example. As per the requirement of an application, we can choose an appropriate data structure.
We learned in detail about this with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on List vs LinkedList in Java.
References
List Interface
LinkedList Class
Stack Class