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.
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.

Set Interface:
A Set is a collection that cannot contain duplicate elements. As you might expect, this interface models the mathematical set abstraction. It is used to represent sets like the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine.

This interface doesn’t contain any new methods.we have to use collection interface methods.

Hash Set:

  • The underlying data structure for the set is Hashtable.
  • Elements are inserted based on the hash code,hence insertion order is not possible.
  • If we are trying to add a duplicate object no chance of getting RTE or CTE add() just simply returns false.
  • Hetrogeneous objects are allowed.
  • Null insertion is possible but only once.
  • HashSet is the best choice for searching operations.
  • Hashset implemented serializable and clonable interface.
1.HashSet h=new HashSet();
Creates a new empty HashSet with default initial capacity 16 and load factor or fill ratio 0.75.
2.HashSet h=new HashSet(int initialcapacity);
Creates an empty HashSet with specified initial capacity and default fill ratio is 0.75.
3.HashSet h=new HashSet(int int itialcapacity,float fillratio)
creates an empty HashSet with the specified initial capacity and specified fill ratio.
4.HashSet h=new HashSet(Collection c)

HashSet Example:
import java.util.*;
class HashSetDemo
public static void main(String arg[])
HashSet h=new HashSet();
System.out.println(h.add(“b”); // true
h.add(new Integer(10));
System.out.println(h.add(“d”); // flase
System.out.println(h); // depends on hash code number insertion order.

Linked Hash Set:
The linked hash set is exactly similar to HashSet Except the following differences.
  • Hashtable is the underlying data structure of hashset.Hashtable and doubly linkedlist are the underlying datastructures for linkedhashset.
In the above demo LinkedHashSet l=new LinkedHashSet();
Then the output: [b,c,d,null,10] preceding with true and flase.i.e insertion order is preserved.
We can use LinkedHashSet for implementing caching memory.


Post a Comment