On the problem of contains comparison value of HashSet's method

look directly at the code, why I directly add a String object contains () returns true, when adding a Dog object, it returns false, and Set is not able to add a repeating element, why can I add two Dog objects and not String objects


class Dog {

String color;
public Dog(String s){
    color = s;
}   

}

public class SetAndHashCode {

public static void main(String[] args) {
    HashSet<Dog> dogSet = new HashSet<Dog>();
    boolean resultq;
    dogSet.add(new Dog("we have white"));
    System.out.println("We have " + dogSet.size() + " white dogs!");
    resultq = dogSet.contains(new Dog("we have white"));
    System.out.println(resultq);
       
    HashSet<String> books = new HashSet<String>();    
    //  
    books.add(new String("Struts2"));  
    books.add(new String("Struts2"));
    boolean result = books.contains(new String("Struts2")); 
    System.out.println("We have " + books.size() + " books!");         
    System.out.println(result);          
    //  
    System.out.println(books);    
}

}
the result of execution is
* * We have 2 white Dogs!
false
We have 1 books!
true
[Struts2 authoritative Guide] * *

Mar.18,2021
The

String object overrides the equeals () and hashCode () methods.
String's hashCode is calculated based on the char array in String. If the char [] array is the same, then hashCode is the same element for HashSet.
for ordinary objects, new is a distinctive, hashCode () that is address-dependent and always different


this question looks directly at the source code. The contains source code uses HashMap's getNdoe method

determines whether it contains another object that satisfies hash equality at the same time and whether it is the same object in memory or whether the equals method is equal. The
String object has overridden the hashCode and equals methods. As long as the value of String is equal, that is, the returned hashcode is equal. Equals returns true.
the Dog object does not write hashCode and equals methods, but uses the method of parent Object. The hashcode for creating different objects is different. Equals returns false, so the if (e.hash = = hash & ((k = e.key) = key | (key! = null & & key.equals (k)) of the above source code is not satisfied. So you can use the color property to override the hashcode and equals methods, and call contains to return true


Set is characterized by weight removal, so it is no problem that key cannot be repeated. There is a point. If the key entering Set is of String type, see what Set has done. First, do a hash calculation on key and get a hashCode value of, Iterator keys for equals comparison. If true is returned, then the key cannot be put in it, achieving the effect of removing weight. If the key entering Set is an object, we all know that new is an object, and if they are equal, then hashCode must also be equal, equals must also be true, but JVM does not like this, and defines that the object out of each new is unique, which causes equals to return false, then Set can put down the Dog object, because you put two Dog objects before and after, rather than a Dog class. I hope I can help you, where the problem is, welcome the gods to correct it!

Menu