List
ArrayList,LinkedList,Vector and Stack are the classes which implements List interface.

Below is the detail explanation of the List implemented classes

ArrayList:

  • Underlying data structure is resizable array or growable array.
  • Duplicate objects are allowed.
  • Insertion order is preserved.
  • Heterogeneous objects are allowed.
  • null insertion is also possible.
  • ArrayList is best choice if frequent operation is retrieval(ArrayList implements RandamAccess interface,hence we can access any element with the same speed.)
  • ArrayList is worst choice if frequent operation is insertion or deletion in the middle(because internally requires several shift operations)

Contructors:

  1. ArrayList list=new ArrayList()
  2. Creates an empty ArrayList object with default initial capacity 10.Once an ArrayList reaches it’s maximum capacity then a new ArrayList object will be created with new capacity
    new capacity=(currentCapacity*3/2)+1

  3. ArrayList list=new ArrayList(int initialCapacity)
  4. Creates an empty ArrayList object with the specified initial capacity.

  5. ArrayList list=new ArrayList(Collection collection)
  6. This is for inter conversion between collection objects.

ArrayList Example:

 
package com.java2learn.collections;
import java.util.ArrayList;
public class ArrayListDemo {

	public static void main(String[] args) {
		ArrayList list=new ArrayList();
		list.add("A");
		list.add(new Integer(100));
		list.add(null);
		list.add("B");
		System.out.println(list);
		list.remove(1);
		list.set(2,"Z");
		System.out.println(list);
	}
}

output:
[A, 100, null, B]
[A, null, Z]

Note:Every Collection implemented class by default implements Serializable and Cloneable interfaces.
you can refer to this ArrayList class API

LinkedList:

  • Underlying data structure is double linkedlist.
  • Duplicate objects are allowed.
  • Insertion order is preserved.
  • Heterogeneous objects are allowed.
  • null insertion is also possible.
  • LinkedList is bestchoice if frequent operation is insertion or deletion in the middle.
  • LinkedList is worst choice if frequent operation is retrieval.
  • LinkedList implements Serializableand Cloneableinterfaces but not RandomAccessinterface.
  • LinkedList is mostly commonly used for implementing Stacks and Queues.To Support this requirement LinkedList class contains the following methods
    addFirst(Object),addLast(Object),removeFirst(),removeLast(),getFirst() and getLast().

Contructors:

  1. LinkedList list=new LinkedList()
  2. Creates an empty LinkedList

  3. LinkedList list=new LinkedList (Collection collection)
  4. This is for inter conversion between collection objects.

LinkedList Example:

 
package com.java2learn.collections;
import java.util.LinkedList;

public class LinkedListDemo {

	public static void main(String[] args) {
		LinkedList linkedList=new LinkedList();
		linkedList.add(".NET");
		linkedList.add(new Integer(999));
		linkedList.add(null);
		linkedList.add(".NET");
		linkedList.set(0,"IT");
		linkedList.add(0,"JAVA");
		linkedList.removeLast();
		linkedList.addFirst("SOFTWARE");
		System.out.println(linkedList);
	}
}

output:
[SOFTWARE, JAVA, IT, 999, null]

you can refer to this LinkedList class API

Vector:

  • Underlying data structure is resizable array or growable array.
  • Duplicate objects are allowed.
  • Insertion order is preserved.
  • Heterogeneous objects are allowed.
  • null insertion is also possible.
  • Vector is best choice if frequent operation is retrieval(Vector implements RandamAccess interface,hence we can access any element with the same speed.)
  • Vector is worst choice if frequent operation is insertion or deletion in the middle.

Contructors:

  1. Vector vector=new Vector()
  2. Creates an empty Vector object with default initial capacity 10.When ever vector reaches it’s maximum capacity a new vector object will be created with double capacity.

  3. Vector vector=new Vector (int initialCapacity)
  4. Vector vector=new Vector (int initialCapacity,int incrementalCapacity)
  5. Vector vector =new Vector (Collection collection)

Vector Example:

package com.java2learn.collections;
import java.util.Vector;

public class VectorDemo {

	public static void main(String[] args) {
		Vector vector=new Vector();
		System.out.println("capacity:"+vector.capacity());
		for(int item=1;item<=10;item++){
			vector.addElement(item);
		}
		System.out.println("capacity:"+vector.capacity());
		vector.addElement("JAVA");
		System.out.println("capacity:"+vector.capacity());
		System.out.println(vector);
	}
}

output:
capacity:10
capacity:10
capacity:20
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, JAVA]

Difference Between Vector and ArrayList

Vector ArrayList
All methods are synchronized. No method is synchronized.
Vector object is thread safe. ArrayList object is not thread safe.
Performance is low. Performance is high.
Introduced in 1.0v and hence it is a legacy class. Introduced in 1.2v and it is not a legacy class.

Note:ArrayList is by default non synchronized but by using Collections class method we can get synchronized version of ArrayList.
public static List synchronizedList(List list)
Similarly, We can get the synchronized versions of Set and Map objects by using the following Collections class methods.
public static Set synchronizedList(Set set)
public static Map synchronizedList(Map map)

you can refer to this Vector class API

Stack:

  • Stack is the child class of Vector and contains only one constructor.
  • Stack stack=new Stack();

  • If the specified object present in the Stack it returns it’s offset from the top of the Stack otherwise it returns -1

Stack Example:

package com.java2learn.collections;
import java.util.Stack;

public class StackDemo {

	public static void main(String[] args) {
		Stack stack=new Stack();
		stack.push("JAVA");
		stack.push(".NET");
		stack.push("PHP");
		System.out.println(stack);
		System.out.println(stack.search("JAVA"));
		System.out.println(stack.search("C++"));
	}
}

Output:
[JAVA, .NET, PHP]
3
-1
You can refer to this Stack class API