Skip to main content

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
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