1
import java.util.HashSet;
import java.util.Set;

   class Employee {
    @Override
      public int hashCode() {
    System.out.println("Hash");
    return super.hashCode();
    }

}

 public class Test2 {

public static void main(String[] args) {
    Set<Employee>set= new HashSet<>();
    Employee employee = new Employee();
    set.add(employee);
    System.out.println(set);// if we comment this "Hash" will be printed once
}
 }

Above code calls hashCode method 2 times if we print set. Why hashcode method is called on System.out.println()?

Java Man
  • 1,854
  • 3
  • 21
  • 43
Ravi Godara
  • 497
  • 6
  • 20
  • The hashCode Method is called when the object is inserted into the HashSet and when the Set is printed to the console. The hashCode is part of the default representation, if an object is printed to the console. – Christian Mar 10 '14 at 07:25
  • He knew that. He would like to know why. – Andre Hofmeister Mar 10 '14 at 07:26
  • I can understand it is called when inserted.But why it is called on Print statement? – Ravi Godara Mar 10 '14 at 07:26
  • 1
    This is because of `Object`'s `.toString()` implementation, which you call here – fge Mar 10 '14 at 07:27
  • possible duplicate of [When HashSet call equal method?](http://stackoverflow.com/questions/21626823/when-hashset-call-equal-method) – Rahul Mar 10 '14 at 07:27
  • Please see my comment-edit above. Override the toString() method of Employee with a custom String returned to see the difference. – Christian Mar 10 '14 at 07:28

3 Answers3

3

Find the following reason for printing Hash two times

  1. For finding the hash value when you insert the Employee into the HashSet

  2. When you print the set, it's calls the hashCode() method inside the default toString() method from Object class.

The default toString() method from Object class API docs says

The toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

See this. In short, the default toString() function calls hashCode() and uses a hexadecimal representation of the hash as part of the String.

Avery
  • 2,270
  • 4
  • 33
  • 35
  • Which if you trace back, follows a similar logic to Object. Note the append call: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/StringBuilder.java#StringBuilder.append%28java.lang.Object%29 which calls valueOf http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.valueOf%28java.lang.Object%29 which calls the linked toString EDIT: Deleted comment linked to http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/AbstractCollection.java#AbstractCollection.toString%28%29 – Avery Mar 10 '14 at 07:37
1

The first call to hashCode() is executed when adding an Employee to your set variable, as it's needed in order to calculate which bucket to put it in.

The second call is a bit sneaker. Any Collection's default toString() is a coma-delimited concatination of all its elements toString()s enclosed by square brackets (e.g., [object1, object2]). Any object's default toString(), if you don't override it is getClass().getName() + "@" + Integer.toHexString(hashCode()). Here, since you don't override Employee's toString(), it's called again when you print set.

Mureinik
  • 297,002
  • 52
  • 306
  • 350