List in Java Collections | Explain List in Collection framework?

 



A List is a subinterface of the Collection interface in the Java Collections framework. It represents an ordered collection of elements, which means that the elements in a List have a specific order and their positions are indexed starting from 0. Lists allow duplicate elements and the elements can be accessed by their position in the list.


There are several classes that implement the List interface, including ArrayList, LinkedList, and Vector.


ArrayList: ArrayList is an implementation of the List interface that uses an array for storage. It provides fast performance for add, remove, and get operations, but slow performance for insert operations. It's a good choice when you need a list that provides fast performance and you don't need to insert elements into the middle of the list.


LinkedList: LinkedList is an implementation of the List interface that uses a linked list for storage. It provides fast performance for insert operations but slow performance for add, remove, and get operations. It's a good choice when you need a list that provides fast performance for insert operations and you don't need to retrieve elements frequently.


Vector: Vector is an implementation of the List interface that uses an array for storage. It's similar to ArrayList, but it's synchronized, which makes it thread-safe. This means that multiple threads can access a Vector at the same time without causing any problems. It provides a slower performance than ArrayList, but it's a good choice when you need a thread-safe list.


Stack: Stack is an implementation of the List interface that uses a linked list for storage. It provides fast performance for add, remove, and get operations, but slow performance for insert operations. It's a good choice when you need a list that provides fast performance and you don't need to insert elements into the middle of the list.


All three classes have the same methods like add, remove, get, set, etc. But the internal implementation is different.


The List interface provides several useful methods for working with lists, such as:


add(int index, E element): Inserts the specified element at the specified position in the list.

get(int index): Returns the element at the specified position in the list.

remove(int index): Removes the element at the specified position in the list.

set(int index, E element): Replaces the element at the specified position in the list with the specified element.

subList(int fromIndex, int toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

indexOf(Object o): Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

In addition to these methods, the List interface also inherits all the methods from the Collection interface, such as add(E e), remove(E e), size(), isEmpty(), contains(E e), etc.



Here is an example that demonstrates the use of each of these List implementations:


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.Stack;
 
public class Main {
    public static void main(String[] args) {
        // Create an ArrayList of Strings
        List<String> arrayList = new ArrayList<String>();
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Cherry");
        System.out.println("ArrayList: " + arrayList);
 
        // Create a LinkedList of Strings
        List<String> linkedList = new LinkedList<String>();
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Cherry");
        System.out.println("LinkedList: " + linkedList);
 
        // Create a Vector of Strings
        List<String> vector = new Vector<String>();
        vector.add("Apple");
        vector.add("Banana");
        vector.add("Cherry");
        System.out.println("Vector: " + vector);
 
        // Create a Stack of Strings
        List<String> stack = new Stack<String>();
        stack.add("Apple");
        stack.add("Banana");
        stack.add("Cherry");
        System.out.println("Stack: " + stack);
    }
}
 


When you run the above code, it will output the following:


ArrayList: [Apple, Banana, Cherry]
 
LinkedList: [Apple, Banana, Cherry]
 
Vector: [Apple, Banana, Cherry]
 
Stack: [Apple, Banana, Cherry]



In this example, we create an ArrayList, LinkedList, Vector, and Stack and add the same elements to each list. When we print the lists, we can see that each list contains the same elements.


It's important to note that each implementation of the List interface has its own strengths and weaknesses, and you should choose the implementation that best meets your needs based on the requirements of your specific use case.


For example, if you need a list that provides fast performance for add, remove, and get operations, then ArrayList or LinkedList may be a good choice. If you need a thread-safe list, then Vector may be a good choice. If you need a list that provides fast performance for insert operations, then LinkedList may be a good choice.


In addition to the implementations of the List interface that I've covered, there are also several other implementations of the List interface in the Java Collection Framework, such as CopyOnWriteArrayList, SubList, and more. You should choose the implementation that best meets your needs based on the requirements of your specific use case.


Post a Comment

Post a Comment (0)

Previous Post Next Post