List vs. ArrayList in Java
In Java, the List interface and the ArrayList class are foundational components of the Java Collection Framework. While they are closely related, it’s crucial to understand the distinctions between them.
List in Java
List is an interface in the Java Collection Framework that represents an ordered collection of elements. It defines a contract for collections that maintains the order of elements and allows access by index. Being an interface, List does not specify how the data is stored or manipulated; instead, it provides a set of operations that can be implemented by various classes.
Key characteristics of List
List has the following characteristics:
Ordered collection:
Listmaintains the order in which elements are added.Allows duplicates:
Listcan contain duplicate elements.Provides random access: Elements in a
Listcan be accessed by index.Flexible implementations:
Listhas multiple implementations, includingArrayList,LinkedList, and others.
Code example
Let’s understand List implementation with ArrayList and LinkedList:
import java.util.List;import java.util.ArrayList;import java.util.LinkedList;public class ListInJava {public static void main(String[] args) {// List with ArrayList implementationList<String> arrayListJava = new ArrayList<>();arrayListJava.add("Jack");arrayListJava.add("Ashley");arrayListJava.add("Martin");System.out.println("ArrayList in Java: " + arrayListJava);// List with LinkedList implementationList<String> linkedListJava = new LinkedList<>();linkedListJava.add("Xander");linkedListJava.add("Yasmine");linkedListJava.add("Zach");System.out.println("LinkedList in Java: " + linkedListJava);}}
Code explanation
Lines 1–3: We import key classes and interfaces used in the code. Specifically, they import
List,ArrayList, andLinkedListfrom the Java Collection Framework.Listis an interface for ordered collections whileArrayListandLinkedListare two common implementations ofList.Line 9: We create a
Listvariable namedarrayListwith anArrayListimplementation. This variable will hold a list of strings.Lines 10–11: Here, we add three elements:
Alice,Bob, andCharlieto thearrayList.Line 15: We create a
Listvariable namedlinkedListwith aLinkedListimplementation. This variable will also hold a list of strings.Lines 16-18: Here, we add three elements:
Xander,Yasmine, andZachto thelinkedList.
ArrayList in Java
ArrayList is a specific implementation of the List interface that uses an underlying array to store elements. It is part of the java.util package and is known for its dynamic resizing capabilities. As elements are added or removed, ArrayList automatically adjusts its size to accommodate changes.
Key characteristics of ArrayList
The following are the characteristics of ArrayList in Java:
Resizable array:
ArrayListautomatically grows or shrinks as elements are added or removed.Fast random access: Since
ArrayListuses an array-based structure, accessing elements by index is fast.Shifting elements: While
ArrayListis efficient for random access, adding or removing elements in the middle of the list can be costly due to the need to shift elements.
Code example
Let’s understand ArrayList implementation with the help of an example:
import java.util.ArrayList;public class ArrayListJava {public static void main(String[] args) {// Create an ArrayList of integersArrayList<Integer> ArrayListOfNumbers = new ArrayList<>();// Add elements to the ArrayListArrayListOfNumbers.add(10);ArrayListOfNumbers.add(20);ArrayListOfNumbers.add(30);System.out.println("ArrayList after adding elements: " + ArrayListOfNumbers);// Removes an element from the ArrayListArrayListOfNumbers.remove(1); // Removes the second element (20)System.out.println("ArrayList after removing an element: " + ArrayListOfNumbers);}}
Code explanation
Line 1: We import the
ArrayListclass from thejava.utilpackage.Line 7: We create an
ArrayListnamednumbersto hold integer values. At this point, the list is empty.Lines 10–12: We add three integer elements to the
numberslist:10,20, and30. This is done using theadd()method, which appends elements to the end of the list.Line 14: Here, we print the current contents of the
ArrayListafter adding the elements.Line 17: We remove an element from the
ArrayListat index1. This removes the second element, which is20.Line 19: This statement prints the
ArrayListafter removing an element.
Differences Between List and ArrayList
|
| |
Nature | |
|
Flexibility |
|
|
Performance | Some |
|
Implementation | The interface of a | The |
Quiz
Test your understanding of this topic. This will help you assess how well you’ve grasped the key concepts we’ve discussed.
What is the output of the following Java code?
import java.util.ArrayList;
import java.util.List;
public class ListMutation {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
List<String> list2 = list1;
list2.add("C");
System.out.println(list1);
}
}
["A", "B"]
["A", "B", "C"]
["C"]
It will throw an exception.
Free Resources