Set

Set

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 *

 

 

 

 

Constructor

  • 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,
                                null,l,C,B,10)
            }
}

Linked HasSet

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

HasSet

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

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. 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