Followers

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 object6.boolean removeAll(Collection c)7.int indexOf(Object o)return the index of first occurrencereturns -1 if there is no such object8.int lastIndexOf(Objetc o)9.Object get(int index)there is no get method in the collection interface10.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.

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 .

LinkedList:

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.

Stack Class:It is the child class of VectorContain only one constructorStack 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.5.int search(Object o)returns the offset from the top of the stack if the Object present else return -1.Ex: s.search(“a”);Stack example:Import java.util.*;Class StackDemo{public static void main(String arg[]){Stack s=new Stack();s.push(“a”);s.push(“b”);s.push(“c”);System.out.println(s); // [a,b,c]System.out.println(s.search(“c”)); // 1System.out.println(s.search(“a”)); // 3}}insertion order should be preserved should be not LIFO.