Important classes implementing List interface in java

Subscribe for New Posts


ArrayList

Constructors in ArrayList

 ArrayList l = new ArrayList();
ArrayList l = new ArrayList(int initialCapacity);
ArrayList l = new ArrayList(Collection c);
Demo Implementation:
import java.util.ArrayList;
public class ArrayListDemo {
    public static void main(String[] args){
        ArrayList l = new ArrayList(); // Warning comes if we're not using generics. To remove use
        // ArrayList<String> l = new ArrayList<String>();
        l.add("A");
        l.add(10); // auto-boxing(primitive to object conversion) performed internally.
        l.add("A");
        l.add(null);
        System.out.println(l); // l is reference to ArrayList object. 
        // If we're trying to print any object reference internally toString() method will be invoked. 
        l.remove(2);
        System.out.println(l); // [A, 10, null]
        l.add(2, "M");
        l.add("N");
        System.out.println(l); // [A, 10, M, null, N]
    }
}

Note 1: ArrayList and Vector classes implements RandomAccess interface, so that any random element we can access with the same speed.

Note 2: RandomAccess interface is present in java.util package and it doesn’t contain any methods. It is a marker interface., where required ability will be provided automatically by the JVM.

Demo Implementation
ArrayList l = new ArrayList();
LinkedList l2 = new LinkedList();
System.out.println(l instanceof Serializable); // true
System.out.println(l2 instanceof Serializable); // true
System.out.println(l instanceof Cloneable); // true
System.out.println(l2 instanceof Cloneable); // true
System.out.println(l instanceof RandomAccess); // true
System.out.println(l2 instanceof RandomAccess); // false
Pros and Cons of using ArrayList
Differences between ArrayList and Vector
ArrayList Vector
1. Every method present in the ArrayList is non-synchronized. 1. Every method present in the Vector is synchronized.
2. At a time, multiple threads are allowed to operate on ArrayList object and hence it is not thread-safe. 2. At a time, only one thread is allowed to operate on Vector object and hence it is thread-safe.
3. Relatively, performance is high because threads are not required to wait to operate on ArrayList object. 3. Relatively, performance is low because threads are required to wait to operate on Vector object.
4. Introduced in 1.2v and it is non-legacy. 4. Introduced in 1.0v and it is legacy class.
How to get Synchronized version of ArrayList object

ArrayList is not synchronized by default. But we can get synchronized version of ArrayList object by using synchronizedList() method of Collections class.

ArrayList l = new ArrayList();
List l1 =  Collections.synchronizedList(l); // l is not-synchronized while l1 is synchronized.

Note: We can get synchronized version of Set and Map object by using the following methods of Collections class i.e. synchronizedSet() and synchronizedMap() methods.

LinkedList

Constructors in LinkedList

LinkedList l = new LinkedList();
LinkedList l = new LinkedList(Collection c);

Specific methods of LinkedList class

void addFirst(Object o);
void addLast(Object o);
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();

Demo Implementation

import java.util.LinkedList;
public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList l = new LinkedList();
        l.add("Mandy8055");
        l.add(30);
        l.add(null);
        l.add("Mandy8055"); // [Mandy8055, 30, null, Mandy8055]
        l.set(0, "Software"); // [Software, 30, null, Mandy8055]
        l.add(0, "Sappy"); // [Sappy, Software, 30, null, Mandy8055]
        l.removeLast(); // [Sappy, Software, 30, null]
        l.addFirst("CCC"); // [CCC,Sappy, Software, 30, null]
        System.out.println(l);
    }
}

Vector

Constructors

Vector v = new Vector();
Vector v = new Vector(int initialCapacity);
Vector v = new Vector(int initialCapacity, int incrementalCapacity);
Vector v = new Vector(Collection c);

Vector-specific methods

void addElement(Object o);
boolean removeElement(Object o);
void removeElementAt(int index);
void removeAllElements();
int size();
int capacity();
Enumeration elements();

Demo Implementation

import java.util.*;
public class VectorDemo1 {
    public static void main(String[] args){
        Vector v = new Vector(); // Warning comes because of lack of type safety. Use generics
//        Vector v = new Vector(10, 5);
//        Vector v = new Vector(24);
        System.out.println(v.capacity());
        for(int i = 1; i <= 10; i++){
            v.addElement(i); // auto-boxing will be performed automatically.
        }
        System.out.println(v.capacity());
        v.add("A");
        System.out.println(v.capacity());
        System.out.println(v);
    }
}

Stack

Stack-specific methods
Object push(Object o);
Object pop();
Object peek();
boolean empty();
int search(Object o); // returns the offset of o if o is present inside the stack else returns -1. 
Implementation Demo
import java.util.Stack;
public class StackDemo {
    public static void main(String[] args){
        Stack s = new Stack();
        s.push("A");
        s.push("B");
        s.push("C");
        System.out.println(s);
        System.out.println(s.search("A"));
        System.out.println(s.search("Z"));
    }
}