Image Source on Google Search

SetInterface

This can be used for representing a group of Individual objects where insertion order is not preserved and duplicate objects are not allowed. Set interface is child interface of Collection.

This interface doesn’t contain any new method and we have to use only collection Interface methods.

HashSet

·   The underlying Data Structure for HashSet is Hashtable.
·   Insertion order is not preserved and it is based on has code of the Object.
·   Duplicate objects are not allowed. Violation leads to no CompileTimeError or RuntimeError, add method simply returns false.
·   ‘null’ insertion is possible.
·  Heterogeneous objects are allowed.
  • HashSet is the best choice if the frequent operation is Search Operation.

Constructors

1. HashSet h = new HashSet()
Creates an empty HashSet object with default initial value 16 and default fill ratio 0.75

2. HashSet h = new HashSet(int initialcapacity)

3. HashSet h = new HashSet(int initialCapacity, float fillratio) Here fillratio is 0 or 1.

4. HashSet h = new HashSet(Collection c)

Ex:

import java.util.*;
class HashSetDemo
{
public static void main(String arg[])
{
HashSet h = new HashSet();
h.add("B");
h.add("C");
h.add("D");
h.add("Z");
h.add(null);
h.add(new Integer(10));
System.out.println(h.add("Z"));
System.out.println(h);
}
}

LinkedHashSet

It is the child class of HashSet. It is Exactly similar to HashSet. Except the following differences.

HashSet
LinkedHashSet
1) The underlying Data Structure is Hashtable
1) The underlying Data Structures are Hashtable and LinkedList
2) Insertion Order is not preserved
2) Insertion Order is preserved.
3) Introduced in 1.2 version
3) Introduced in 1.4 version

In the above program if u r replacing ‘HashSet’ with ‘LinkedHashSet’ the following is the output.

Note:- For implementing caching application the best suitable Data structure is LinkedHashSet and LinkedHashMap where duplicate objects are not allowed and insertion order Must be preserved.

SortedSet

This can be used for representing a group of individual objects where duplicate objects are not allowed.Insertion order is not preserved but all the elements are inserted according to some sorting order of elements. The sorting order may be default natural sorting order or customized sorting order.

SortedSet Interface contains the following more specific methods

1. Object first()
Returns first element in the SortedSet

2. Object last()
3. SortedSet headSet(Object obj)
Returns the SortedSet contains the elements which are less than object obj.
4. SortedSet tailSet(Object obj)
Returns the SortedSet whose elements are greater than or equal to object obj
5. SortedSet subSet(Object obj1, Object obj2)
Returns the SortedSet whose elements are >= obj1 but < Obj2

6. Comparator comparator();
Returns the comparator object describes the underlying sorting technique. If you are using default(Assending) natural sorting order it returns null.

Observe the following Example.

          100
          120
          130
          140
          150
          160

first() à 100
last() à 160
headSet(140) à{100,120,130}
tailSet(140) à {140,150,160}
subset(130,150)à{130,140}
comparator() à null

TreeSet

·   The underlying Data structure for the TreeSet is Balanced tree.
·   Duplicate objects are not allowed. If we are trying to insert duplicate object we won’t    get any compile time error or Run time error, add method simply returns false.
·    Insertion order is not preserved but all the elements are inserted according to some sorting order.
·    Heterogeneous objects are not allowed, violation leads to Run time error saying class cast Exception
    
Constructors

1. TreeSet t = new TreeSet();
Creates an empty TreeSet Object where the sorting order is default natural sorting order.

2. TreeSet t= new TreeSet(Comparator c)
Creates an empty TreeSet Object where the Sorting order is specified by comparator object.

3. TreeSet t= new TreeSet(Collection c)
4. TreeSet t= new TreeSet(SortedSet s)
Creates TressSet Object for a given SortedSet.

Ex:

import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet();
t.add("A");
t.add("B");
t.add("Z");
t.add("L");
//t.add(new Integer(10)); àClassCastException.
//t.add(null); à NullPointerException.
t.add("A"); à false.
System.out.println(t);
}
}

null Acceptance

For the empty TreeSet as the first element null insertion is possible. But after inserting null if we are trying to insert any other element we will get NullPointerException.

If the TreeSet already contains some elements if we are trying to insert null we will get NullPointerException.

Ex:
import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet();
t.add(new StringBuffer("A"));
t.add(new StringBuffer("B"));
t.add(new StringBuffer("T"));
t.add(new StringBuffer("Z"));
System.out.println(t);
}
}
O/P:- R.E: ClassCastException.

If we are depending on natural sorting order compulsory the objects should be homogeneous and comparable other wise we will get class cast Exception.

An object is said to be comparable(if and only if) the corresponding class has to implement comparable interface.

All wrapper classes and String class already implemented comparable interface. But the String buffer doesn’t implement comparable interface. Hence in the above program we got class cast exception.

Post a Comment

 
Top