Java List Interface
Java List Interface
Java Course Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithrea
The List Interface in Java extends the Collection Interface and is a part
of java.util package. It is used to store the ordered collections of
elements. So in a Java List, you can organize and manage the data
sequentially.
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 1/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Output
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 2/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Syntax:
ArrayList and LinkedList are the most widely used due to their
dynamic resizing and efficient performance for specific operations.
Stack is a subclass of Vector, designed for Last-In-First-Out (LIFO)
operations.
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 3/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Now let us discuss the operations individually and implement the same
in the code to grasp a better grip over it.
1. Adding Elements
In order to add an element to the list, we can use the add() method.
This method is overloaded to perform multiple operations based on
different parameters.
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 4/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Example:
Java
Output
2. Updating Elements
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 5/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Example:
Java
Output
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 6/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
3. Searching Elements
Parameters:
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 7/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Output
4. Removing Elements
Parameters:
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 8/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 9/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
38 }
Output
5. Accessing Elements
In order to access an element in the list, we can use the get() method,
which returns the element at the specified index
Parameters:
get(int index): This method returns the element at the specified index
in the list.
Example:
Java
Output
Geeks
For
Geeks
[Geeks, For, Geeks]
Parameters:
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 11/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Output
Adding Element in
O(1) O(1)
List Interface
Remove Element
O(N) O(N)
from List Interface
Replace Element in
O(N) O(N)
List Interface
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 12/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Traversing List
O(N) O(N)
Interface
Methods: There are multiple ways to iterate through the List. The most
famous ways are by using the basic for loop in combination with a get()
method to get the element at a specific index and the advanced for a
loop.
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 13/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Output
Method Description
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 14/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Method Description
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 15/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Method Description
List Set
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 16/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
List Set
Multiple null elements can be The null element can store only
stored. once.
Now let us discuss the classes that implement the List Interface for
which first do refer to the pictorial representation below to have a better
understanding of the List interface. It is as follows:
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 17/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
set, remove, etc.) are implemented by making a fresh copy of the list.
3. AbstractSequentialList: This class implements the Collection
interface and the AbstractCollection class. This class is used to
implement an unmodifiable list, for which one needs to only extend
this AbstractList Class and implement only the get() and the size()
methods.
ArrayList
Vector
Stack
LinkedList
Let us discuss them sequentially and implement the same to figure out
the working of the classes with the List interface.
1. ArrayList
Example:
Java
12 int n = 5;
13
14 // Declaring the List with initial size n
15 List<Integer> arrli = new ArrayList<Integer>
(n);
16
17 // Appending the new elements
18 // at the end of the list
19 for (int i = 1; i <= n; i++)
20 arrli.add(i);
21
22 // Printing elements
23 System.out.println(arrli);
24
25 // Remove element at index 3
26 arrli.remove(3);
27
28 // Displaying the list after deletion
29 System.out.println(arrli);
30
31 // Printing elements one by one
32 for (int i = 0; i < arrli.size(); i++)
33 System.out.print(arrli.get(i) + " ");
34 }
35 }
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
2. Vector
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 19/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Example:
Java
Output
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 20/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
3. Stack
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 21/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
4. LinkedList
Example:
Java
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 22/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
7
8 class GFG {
9 public static void main(String[] args)
10 {
11 // Size of the LinkedList
12 int n = 5;
13
14 // Declaring the List with initial size n
15 List<Integer> ll = new LinkedList<Integer>
();
16
17 // Appending the new elements
18 // at the end of the list
19 for (int i = 1; i <= n; i++)
20 ll.add(i);
21
22 // Printing elements
23 System.out.println(ll);
24
25 // Remove element at index 3
26 ll.remove(3);
27
28 // Displaying the list after deletion
29 System.out.println(ll);
30
31 // Printing elements one by one
32 for (int i = 0; i < ll.size(); i++)
33 System.out.print(ll.get(i) + " ");
34 }
35 }
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
https://www.geeksforgeeks.org/list-interface-java-examples/?ref=next_article 23/29
12/24/24, 7:16 PM Java List Interface - GeeksforGeeks
Similar Reads
Why to Use Comparator Interface Rather than Comparable Interface…
In Java, the Comparable and Comparator interfaces are used to sort
collections of objects based on certain criteria. The Comparable interface…
9 min read