Java List vs Set
Both the List interface and the Set interface inherits the Collection interface. However, there exists some differences between them.
List |
Set |
---|---|
The List is an ordered sequence. | The Set is an unordered sequence. |
List allows duplicate elements | Set doesn’t allow duplicate elements. |
Elements by their position can be accessed. | Position access to elements is not allowed. |
Multiple null elements can be stored. | The null element can store only once. |
List implementations are ArrayList, LinkedList, Vector, Stack | Set implementations are HashSet, LinkedHashSet. |
Classes Association with a Java List Interface
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:
AbstractList, CopyOnWriteArrayList, and the AbstractSequentialList are the classes that implement the List interface. A separate functionality is implemented in each of the mentioned classes. They are as follows:
- AbstractList: 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.
- CopyOnWriteArrayList: This class implements the list interface. It is an enhanced version of ArrayList in which all the modifications(add, set, remove, etc.) are implemented by making a fresh copy of the list.
- 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.
We will proceed in this manner.
- 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
An ArrayList class which is implemented in the collection framework provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. Let’s see how to create a list object using this class.
Example:
Java
// Java program to demonstrate the // creation of list object using the // ArrayList class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of ArrayList int n = 5 ; // Declaring the List with initial size n List<Integer> arrli = new ArrayList<Integer>(n); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) arrli.add(i); // Printing elements System.out.println(arrli); // Remove element at index 3 arrli.remove( 3 ); // Displaying the list after deletion System.out.println(arrli); // Printing elements one by one for ( int i = 0 ; i < arrli.size(); i++) System.out.print(arrli.get(i) + " " ); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
2. Vector
Vector is a class that is implemented in the collection framework implements a growable array of objects. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. Vectors basically fall in legacy classes but now it is fully compatible with collections. Let’s see how to create a list object using this class.
Example:
Java
// Java program to demonstrate the // creation of list object using the // Vector class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the vector int n = 5 ; // Declaring the List with initial size n List<Integer> v = new Vector<Integer>(n); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) v.add(i); // Printing elements System.out.println(v); // Remove element at index 3 v.remove( 3 ); // Displaying the list after deletion System.out.println(v); // Printing elements one by one for ( int i = 0 ; i < v.size(); i++) System.out.print(v.get(i) + " " ); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
3. Stack
Stack is a class that is implemented in the collection framework and extends the vector class models and implements the Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search and peek. Let’s see how to create a list object using this class.
Example:
Java
// Java program to demonstrate the // creation of list object using the // Stack class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the stack int n = 5 ; // Declaring the List List<Integer> s = new Stack<Integer>(); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) s.add(i); // Printing elements System.out.println(s); // Remove element at index 3 s.remove( 3 ); // Displaying the list after deletion System.out.println(s); // Printing elements one by one for ( int i = 0 ; i < s.size(); i++) System.out.print(s.get(i) + " " ); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
4. LinkedList
LinkedList is a class that is implemented in the collection framework which inherently implements the linked list data structure. It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. Let’s see how to create a list object using this class.
Example:
Java
// Java program to demonstrate the // creation of list object using the // LinkedList class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the LinkedList int n = 5 ; // Declaring the List with initial size n List<Integer> ll = new LinkedList<Integer>(); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) ll.add(i); // Printing elements System.out.println(ll); // Remove element at index 3 ll.remove( 3 ); // Displaying the list after deletion System.out.println(ll); // Printing elements one by one for ( int i = 0 ; i < ll.size(); i++) System.out.print(ll.get(i) + " " ); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
List Interface in Java with Examples
The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.
Table of Content
- List Interface in Java
- Declaration of Java List Interface
- Example of Java List
- Operations in a Java List Interface
- Complexity of List Interface in Java
- Iterating over List Interface in Java
- Methods of the List Interface
- Java List vs Set
Contact Us