Understanding contains method of Java HashSet
问题 Newbie question about java HashSet Set<User> s = new HashSet<User>(); User u = new User(); u.setName("name1"); s.add(u); u.setName("name3"); System.out.println(s.contains(u)); Can someone explain why this code output false ? Moreover this code does not even call equals method of User. But according to the sources of HashSet and HashMap it have to call it. Method equals of User simply calls equals on user's name. Method hashCode return hashCode of user's name 回答1: If the hash code method is