HashSet class

 

see  Collection interface  for descriptions and examples of all of HashSet's methods

 

 

■ This class implements the Set Interface, and extends the AbstractSet class.  It is for everyday Set usage.

 

HashSet  is for unordered Sets (with no duplicates allowed) in random or indeterminate order. So you can't control the order here.  i.e. The snippet below will print  1 3 2  or  3 2 1, depending on your JVM, not  1 2 3  which is the order in which the three items were entered.

 

import java.util.*;

 

Set hs = new HashSet( );

hs.add("1");

hs.add("2");

hs.add("3");

System.out.println( hs );     

 

  HashSet’s everyday constructors:

 

HashSet hs = new HashSet( );                            creates an empty set

HashSet hs = new HashSet(Collection);           creates a set from any Collection