Wednesday, April 1, 2009


Collection Interface :
The Collection interface is the root of the collection hierarchy. A Collection represents a group of objects, known as its elements. Some Collection implementations allow duplicate elements and others do not. Some are ordered and others unordered. The JDK doesn't provide any direct implementations of this interface: It provides implementations of more specific subinterfaces like Set and List. This interface is the least common denominator that all collections implement. Collection is used to pass collections around and manipulate them when maximum generality is desired.

It defines general methods ,which can be used for a group of individual objects.i.e a collection represents a group of individual ibjects.
Note: Collection is an interface to represent a group of individual objects where as collections is an utility class for defining utility methods like sorting,searching… etc.

List Interface:

  • It represents a group of individual objects where
  • Insertion order is preserved via indexes
  • Duplicate objects are allowed. .we can differentiate duplicate objects by using indexes.
  • A list is also known as sequence ArrayList ,LinkedList ,Vector and Stack implement List interface.
Following are the methods list interface :
1.boolean add(Object obj)
2.boolean add(int index,Object o)
3.boolean addAll(Collection c)
4.boolean addAll(int index,Collection c)
5.boolean remove(object o)
remove the first occurrence of this object
6.boolean removeAll(Collection c) indexOf(Object o)
return the index of first occurrence
returns -1 if there is no such object lastIndexOf(Objetc o)
9.Object get(int index)
there is no get method in the collection interface
10.Object set(int index , Object o)
11.ListIterator listIterator()
12.Object remove(int index)

Vector class:
  • The underlying datastructure for vector is growable array or resizable array.
  • Insertion order is preserved.
  • Null insertion is possible.
  • Duplicate objects are allowed.
  • Hetrogeneous objects are allowed.
  • It implemented RandomAccess,Clonable and Serializable interfaces.
  • Best choice for retrieval operation.
  • Worst choice for insertion or deletion in the middle.
When compare with ArrayList,Vector is preferable when thread safety is reqired because all the vector class methods are synchronized.

Vector methods:
For adding objects
1.add(Object o) ----> from collection
2.add(int index, Object o) ---------> from list
addElement(Object o) ---------> Vector
for removing objects
1.remove(Object o) ---------> from collection
2.remove(int index) -----------> from list
3.removeElement(Object o) ----------> Vector.
4.removeElementat(int index) -------------> Vector
6.clear() ------------> from collection
For accessing objects.
1.Object get(int index)
2.Object elementAt(int index)
3.Object firstElement()
4.Object lastElement(); size() capacity();

Constructors of Vector:
1.Vector v=new Vector()
create an empty Vector with default initial capacity 10.
If the Vector reaches its maximum capacity a new Vector object will create with a capacity = current capacity * 2 i.e doubles.
2.Vector v=new Vector(int initialcapacity)
Constructs an empty vector with the specified capacity.
3.Vector v=new Vector(Collection c)
4.Vector v=new Vector(int initialcapacity,int incrementalcapacity)

Vector Example:
import java.util.*;
class VectorDemo
public static void main(String arg[])
Vector v=new Vector();
for(int i=0;i<10;i++)
v.addElement(new Integer(i));
System.out.println(v.capacity()); // 10
System.out.println(v.capacity()); // 20

Array List:
  • The underlying datastructure for array List is growable array or resizable array.
  • Insertion order is preserved.
  • Duplicate objects are allowed.
  • Hetrogeneous objects are allowed.
  • Null insertion is possible any number of times.
Constructors of ArrayList:
1.ArrayList a=new ArrayListA();
creates an empty arraylist with the default initial capacity 10.
If arraylist reaches its maximum capacity ,it creates a new arraylist object with the new capacity as (Current capacity * 3/2 ) + 1;
2.arrayList a=new ArrayList(int initialcapacity)
creates an empty arraylist with the specified initial capacity.
3.ArrayList a=new ArrayList(Collection c)
Construct an equivalent arraylist for the given collection object.

ArrayList Example:
import java.util.*;
class ArraylistDemo
public static void main(String arg[])
ArrayList a=new ArrayList();
a.add(“new Integer(10)”);
System.out.println(a); // [aaa, ccc,bbb,10,null]
every collection class implemented clonable and serializable interfaces.
System.out.println(a instanceOf; // true
System.out.println(a instanceOf; // true

  • ArrayList and Vector classes implemented Random Access interface.Any element in ArrayList and Vector we can access with same speed.Hence ArrayList is best suitable for retrivel operation.
  • ArrayList is a non-synchronised one introduced in 1.2 version.It is a non-legacy class which has high performance.But we can’t achieve security.Where as vector is a synchronized in 1.0 version .It is a legacy class which has low performance But we can achive security.
  • In ArrayList ,there is a possibility for data corruption as it is thread safe.ArrayList is the worrest choice,If your frequent operation is insertion or deletion in the middle,because it reqires so many internal shift operations .
  • The underlying data structure for LinkedList is DoublyLinkedList.
  • Insersion order is preserved.
  • Duplicate objects are allowed.
  • Null insertion is possible any number of times.
  • Hetrogeneous objects are also allowed.
  • It implemented serializable and clonable interfaces but not RandomAccess interface.
  • LinkedList is the best choice if your frequent operation is insertion or deletion in the middle.
  • LinkedList the worest choice if your frequent operation retrieval operation.
LinkedList class contain the following methods for implementing Stacks and Queues.
1.void addFirst(Object o)
2.void addLast(Object o)
3.Object removeFirst()
4.Object removeLast()
5.Object getFirst()
6.Object getLast()

Constructors of LinkedList:
1.LinkedList l=new LinkedList();
create an empty LinkedList (initial capacity is not applicable) size != capacity
2.LinkedList l=new LinkedList(Collection c)

LinkedList Example
import java.util.*;
class LinkedListDemo
public static void main(String arg[])
LinkedList l=new LinkedList();
l.add(new Integer(10));
System.out.println(l); // [ccc,gorantla,scjp,10,null]

Stack Class:
It is the child class of Vector
Contain only one constructor
Stack s=new Stack();
Methods of stack class:
1.Object push (Object obj)
It pushes an element into stack and that element also return.
2.Object pop()
removes the top of the stack and the object is returned.
3.Object peek()
returns the element present on the top of the stack.
4.boolean empty()
returns true if the stack isempty otherwise false. search(Object o)
returns the offset from the top of the stack if the Object present else return -1.
Stack example:
Import java.util.*;
Class StackDemo
public static void main(String arg[])
Stack s=new Stack();
System.out.println(s); // [a,b,c]
System.out.println(“c”)); // 1
System.out.println(“a”)); // 3
insertion order should be preserved should be not LIFO.


Anonymous said...

ok this answer is good but plz tell the where really use ?

Post a Comment