How to calculate hashcode of java hashset

Viewed 31
HashSet<String> set = new HashSet<String>();

set.add("amit");
set.add("kushal");
set.add("vikas");
set.add("ravi");

Iterator<String> i=set.iterator();  
while(i.hasNext())  
{  
    System.out.println(i.next());  
}  

// Output

kushal
ravi
amit
vikas
1 Answers

You can simply print the hashCode of your HashSet by calling hashCode() function

 System.out.println("HashCode value: "+ set.hashCode());

Here set is your HashSet which has a function hashCode which is called.

The hashCode() method of HashSet in Java is used to get the hashCode value for this instance of the HashSet. It returns an integer value which is the hashCode value for this instance of the HashSet.

Source: More info

Related