It is the child interface of collection. If insertion order is not required & duplicate objects are not allowed. Then we should go for set interface. Set interface doesn’t contain any new method, hence we have to use only collection interface method.

* image *






  • HasSet h = new HasSet () // creates an empty HasSet object with default initial capacity 16 & default fill ration load factory is 0.75.
  • HasSet h = new HasSet ( int initial capacity);
  • HasSet h = new HasSet ( int initial capacity, float fill ratio // 0 to 1);
  • HasSet h = new HasSet ( collection c)

Load Factor or Fill Ratio:-

import java. util. * ;
Class ExonHasSet
public static void main ( String[] args )
            HasSet h = new HasSet ();
            h. add ("B" );
            h. add ("C" );
            h. add ("2" );
            h. add (null);
            h. add (10);
            h. add (K);
            s.o.p ( h.add ( "Z" )); // false
            s.o.p ( h ); // ( in random order elements will be displayed Z,D,K,

Linked HasSet

It is child class of HasSet. It is exactly same as HasSet, except the following differences.


Linked HasSet

  • The underlying data structure is HasSet.
  • Insertion order is not preserved.
  • Introduced in 1.2


  • The underlying data structure is a combination.
  • Insertion order is preserve.
  • Introduced in version (1.4)

In the above pregame, if we are replacing HasSet with LinkedHasSet is the ( B C Z Null 10 K ), inserting order is preserve. LinkedHasSet & LinkedHasMaps are best suitable to implement cache applications.

The Author


Hello! I am Narayanaswamy founder and admin of I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply