Hashset

Hashset

  • Hashset is a type of set which implement set interface
  • It implement two marker interface

1. Serializable
2. Clonable

  • Hashset is implemented using hash table data structure.
  • Hash set does not allow duplicate object. Hash set inserts object based on the hash code,
  •  Does not preserve insertion order.
  • Null insertion is allowed.
  • Hash set is preferable whenever frequent operation is searching, since the object searching based on hash code.

Constructor are overloaded IN Hash set

— Hashset
— Hashset(initial capacity)
— Hashset(collection c1)
— Hashset(int initialcapacity,float loadfactor)

java Program for Hashset

public class Hashset 
{
public static void main(String[] args) 
{
	HashSet h = new HashSet();
        h.add("Bhavesh");
        h.add("Hardik");
        h.add("vivek");
        h.add("abhi");
        h.add("milan");


      System.out.println(h.size());


      Iterator A = h.iterator();

      while(A.hasNext())
       {
    	  System.out.println(A.next());
       }
   
  }
}

output:

5
Hardik
vivek
milan
Bhavesh
abhi

Leave a Reply