What are the class implementations of List interface in Java?


This article will help you understand what the key features of Java Programming language are.

Let us revise what is Interface in Java.

INTERFACE

Similar to an object, an Interface is a blueprint of a class. It consists of static constants and abstract methods. It is a mechanism to achieve abstraction and multiple inheritance in Java. It is declared using the interface keyword. It provides total abstraction, meaning all methods in an interface must be declared with empty body, and all fields must be public, static and final by default.

Syntax

interface <interface_name> {
   // constant fields declaration
   // abstract methods declaration
   // by default
} 

LIST INTERFACE

The List interface in Java is a way of storing the ordered collection. It is considered as a child interface of Collection. Duplicate values can be stored in a List interface. It preserves the insertion order, allowing inserting of elements and element position accessing. This interface is found in the java.util package.

Syntax

public interface List<E> extends Collection<E> ;

CLASS IMPLEMENTATION OF LIST INTERFACE

Let us discuss about the classes which implements the list interface. They are as follows −

AbstractList

The AbstractList class is used to create an unmodifiable list. To create such a list, one needs to extend this AbstractList class and implement the get() and size() methods only.

AbstractSequentialList

The AbstractSequentialList is used to implement the Collection interface and AbstractCollection class. It is similar to that of the AbstractList. It also creates an unmodifiable list.

ArrayList

This class is implemented in the collection framework. It provides us with dynamic arrays in Java. It can be slower than standard arrays but has more advantages for programs where multiple manipulation in the array is needed.

Example

Creation of List object using Array List Class

import java.io.*; import java.util.*; public class ListObjArrayList{ public static void main(String[] args){ int n = 10; // initializing size of array list List<Integer> arr = new LinkedList<Integer>(); // declaring the list with initial size n for(int i = 1;i <= n;i++){ arr.add(i); // adding elements in the list } System.out.println(arr); // printing the list arr.remove(5); // removing the element at index 5 of the list System.out.println(arr); // printing the new list after deletion for(int j = 0;j < arr.size();j++){ System.out.print(arr.get(j) + " "); // printing the elements one by one } } }

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
1 2 3 4 5 7 8 9 10

CopyOnWriteArrayList

This class implements the list interface. This class is an upgraded version of the ArrayList class where all modifications like add, set, remove, etc. are implemented by making a new copy of the list.

LinkedList

This class is implemented in the collection framework. It inherently implements the linked list data-structure. This class has characteristics of a linear data structure, where every element is a separate object with its individual data and address part. Each element is linked with the help of pointers and addresses. Insertion and deletion operations are easy to perform here, that’s why they are preferred over the arrays.

Example

Creation of List object using Linked List Class.

import java.io.*; import java.util.*; public class ListObjLinkedList{ public static void main(String[] args){ int n = 10; // initializing size of linked list List<Integer> link = new LinkedList<Integer>(); // declaring the list with initial size n for(int i = 1;i <= n;i++){ link.add(i); // adding elements in the list } System.out.println(link); // printing the list link.remove(5); // removing the element at index 5 of the list System.out.println(link); // printing the new list after deletion for(int j = 0;j < link.size();j++){ System.out.print(link.get(j) + " "); // printing the elements one by one } } }

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
1 2 3 4 5 7 8 9 10

Stack

This class is implemented in the collection framework and extends the vector class. It implements the Stack data structure. This class works on the principle Last in First out (LIFO). Other than the basic push and pop operations, this class also provides the empty, search and peek functions.

Example

Creation of List object using Stack Class

import java.io.*; import java.util.*; public class StackObj{ public static void main(String[] args){ int n = 10; // initializing size of stack List<Integer> st = new Stack<Integer>(); // declaring the list with initial size n for(int i = 1;i <= n;i++){ st.add(i); // adding elements in the list } System.out.println(st); // printing the list st.remove(5); // removing the element at index 5 of the list System.out.println(st); // printing the new list after deletion for(int j = 0;j < st.size();j++){ System.out.print(st.get(j) + " "); // printing the elements one by one } } }

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
1 2 3 4 5 7 8 9 10

Vector

This class is implemented in the collection framework. It implements grow able array of objects, which can also be called as a dynamic array. This array can grow or shrink as required. Vectors belong in Legacy classes of Java, but currently they are fully compatible with collections.

Example

Creation of List object using Vector Class

import java.io.*; import java.util.*; public class ListObjVector{ public static void main(String[] args){ int n = 10; // initializing size of vector List<Integer> vec = new Vector<Integer>(n); // declaring the list with initial size n for(int i = 1;i <= n;i++){ vec.add(i); // adding elements in the list } System.out.println(vec); // printing the list vec.remove(5); // removing the element at index 5 of the list System.out.println(vec); // printing the new list after deletion for(int j = 0;j < vec.size();j++){ System.out.print(vec.get(j) + " "); // printing the elements one by one } } }

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
1 2 3 4 5 7 8 9 10

Updated on: 05-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements