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 JavaList
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.
List
List
has the following characteristics:
Ordered collection: List
maintains the order in which elements are added.
Allows duplicates: List
can contain duplicate elements.
Provides random access: Elements in a List
can be accessed by index.
Flexible implementations: List
has multiple implementations, including ArrayList
, LinkedList
, and others.
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);}}
Lines 1–3: We import key classes and interfaces used in the code. Specifically, they import List
, ArrayList
, and LinkedList
from the Java Collection Framework. List
is an interface for ordered collections while ArrayList
and LinkedList
are two common implementations of List
.
Line 9: We create a List
variable named arrayList
with an ArrayList
implementation. This variable will hold a list of strings.
Lines 10–11: Here, we add three elements: Alice
, Bob
, and Charlie
to the arrayList
.
Line 15: We create a List
variable named linkedList
with a LinkedList
implementation. This variable will also hold a list of strings.
Lines 16-18: Here, we add three elements: Xander
, Yasmine
, and Zach
to the linkedList
.
ArrayList
in JavaArrayList
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.
ArrayList
The following are the characteristics of ArrayList
in Java:
Resizable array: ArrayList
automatically grows or shrinks as elements are added or removed.
Fast random access: Since ArrayList
uses an array-based structure, accessing elements by index is fast.
Shifting elements: While ArrayList
is efficient for random access, adding or removing elements in the middle of the list can be costly due to the need to shift elements.
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);}}
Line 1: We import the ArrayList
class from the java.util
package.
Line 7: We create an ArrayList
named numbers
to hold integer values. At this point, the list is empty.
Lines 10–12: We add three integer elements to the numbers
list: 10
, 20
, and 30
. This is done using the add()
method, which appends elements to the end of the list.
Line 14: Here, we print the current contents of the ArrayList
after adding the elements.
Line 17: We remove an element from the ArrayList
at index 1
. This removes the second element, which is 20
.
Line 19: This statement prints the ArrayList
after removing an element.
|
| |
Nature | |
|
Flexibility |
|
|
Performance | Some |
|
Implementation | The interface of a | The |
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