List
As shown earlier in figure of Collection Framework Hierarchy that Collection Interface is a root Interface of the java collection classes. List Interface extends the Collection Interface. List means that it cares about index of the object. There are three classes that implements the List Interface.
- ArrayList
- Vector
- LinkedList
Difference between ArrayList, Vector and LinkedList
| ArrayList | Vector | LinkedList |
|---|---|---|
| ArrayList is a growable array. | same as ArrayList. | same as ArrayList, except that the element are doubly-linked to one another. |
| It give us a fast iteration and fast random access. | slower than ArrayList. | LinkedList may iterate more slowly than ArrayList. |
| ArrayList methods are not synchronized. | Vector methods are synchronized for thread safety. | LinkedKist methods are also not synchronized. |
Syntax of ArrayList, Vector and LinkedList
| List | Create | Add | Traverse |
|---|---|---|---|
| ArrayList | ArrayList name=new ArrayList(); | name.add("name1"); name.add("name2"); | name.get(0); |
| Vector | Vector name=new Vector(5); | same as above+ name.addElement("name1"); | name.get(0); |
| LinkedList | LinkedList name=new LinkedList(5); | same as ArrayList+ name.addFirst("name1"); name.addLast("lastName") | same as ArrayList+ name.getFirst(); name.getLast(); |
Example to show ArrayList insertion :
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo{
String name;
String address;
Integer id;
public ArrayListDemo(String name,String address, int id){
this.name=name;
this.address=address;
this.id=id;
}
public String toString(){
return "name = "+name+" address = "+address+" id = "+id;
}
public static void main(String[] args) {
ArrayListDemo a1=new ArrayListDemo("Peter","UK",25000);
ArrayListDemo a2=new ArrayListDemo("James","USA",35000);
ArrayList list=new ArrayList();
list.add(a1);
list.add(a2);
System.out.println(list);
}
}



