List Interface And Its Classes

List Interface is provided by the Collection framework. It was introduced in the Java 1.2 version. It is basically used for storing and modifying the set of objects. There are mainly 3 interfaces in the collection i.e List Interface, Set Interface, Queue Interface. We can say that Collection is a parent of List, Set, and Queue interfaces.

Collection of Books

List Interface:-

List Interface is used for storing ordered collections. We can store and update the elements dynamically. The list mainly depends on the indexing method. Thus, We can access, delete, modify the elements using indexing. Duplicate as well as Null values can also be stored in the list. In List interface, we can also access by importing the package java. util and from that, we have to inherit the collection.

Classes of List Interface:-

There are mainly 4 classes of List Interfaces.

  1. ArrayList.
  2. LinkedList.
  3. Stack.
  4. Vector.

To begin with the List interface we first declare the list using

List<data_type>List_name=new Arraylist();

List<data_type>List_name=new LinkedList();

List<data_type>List_name=new Stack();

List<data_type>List_name=new Vector();

Methods of List Interface: – 

1)add():-It simply add the element at the end of the list

2)add(int index, Elem):-To add the element at the given position into the list.

3)remove(Elem):- Remove the element from the given list.

4)remove(int index):-Remove the element which is at the given index.

5)isEmpty():-To check the List is empty or not, returns true if empty otherwise returns false.

6)size():-It returns the size of the list.

7)clear():-It clear all the elements from the list.

 Classes

1. ArrayList:

ArrayList is also called a Dynamic array. It is nothing but an object in java. ArrayList does not support primitive types of data means we can not use int, double, long, float. To overcome this we can use Integer class which extends the number object similarly for Double, Long, Float. We can say that these classes are a wrapper class of primitive data types.

Output:-

2) LinkedList

In LinkedList, the elements are store using a doubly-linked list. We use LinkedList over ArrayList because manipulating the data is faster in LinkedList than ArrayList.It uses an address and pointer to perform operations. Here every element is also called Node. The first element is called the head and the last element is called a tail. Each node is associated with its address and pointer. Pointer gives the location(address) of the next element. 

Output:-

3) Stack  

It is also a linear data structure. This element can be get insert or delete from one end and this end is the top of the stack. It follows the LIFO approach which means Last In First Out. In order to add elements into the stack Push method is used and to remove the elements from the stack Pop method is used.

Stacks are use as in the implementation of system procedures such as compilation & program control.

Output:-

4) Vector

Vector is similar to the array we can access the elements using indexing. But the difference is a vector is Synchronize. It is dynamic in nature means if the capacity exceeds then the vector increases its capacity up to 100%.To add elements in the vector along with the add method addElement method is also used. We can initialize the vector class in 4 ways

1) Vector<>V1=new Vector<>();

Initially max capacity is 10 by default.

2) Vector<>V1=new Vector<>(int capacity)

Here, we can thus initialize the capacity of the Vector.

3) Vector<>V1=new Vector<>(int capacity,int incr)

Capacity is initialize and the increment is also initialize by incr. If the capacity of the vector exceeds then increment size is adds to the actual vector. 

4) Vector<>V1=newVector<>(Collection c)

It contains the elements of the collection c.

Output:-

Advantages of List Interface:-

  1. It is Flexible.
  2. We can do multiple inheritances using the interface.
  3. It also works on duplicate elements
  4. Also Provides Dynamic nature.

Disadvantages of List Interface:-

  1. Slower in speed than other interfaces.
  2. Only used for an ordered set of collections.

Conclusion:- 

In Java, the collection framework provides the list Interface. So, This list interface came up with a lot of features we can do various operations on elements. It improves efficiency and along with that, it is also easy to maintain. Hence we conclude that the list interface is the best way to store or to perform operations on an ordered set of collections. In this article, we also studied and discussed the list interface and its classes in java.

Written by: Suraj Raut

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *