java - hashCode method called for System.out.println() method in HashSet -
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 comment "hash" printed once } }
above code calls hashcode method 2 times if print set. why hashcode method called on system.out.println()?
see this. in short, default tostring() function calls hashcode() , uses hexadecimal representation of hash part of string.
Comments
Post a Comment