This is one of the most error-prone Java pen tests I’ve ever seen, and without a very deep understanding of Set, it will be 100% wrong.

So let’s take a look at what’s wrong with this problem.

The title is as follows:

Defines a Person class that overrides the hashCode and equals methods, as shown below:

public class Person {    public int id;    public String name;    public Person(int id, String name) {        this.id = id;        this.name = name;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if(o == null || getClass() ! = o.getClass())return false;        Person person = (Person) o;        if(id ! = person.id)return false;        returnname ! = null ? name.equals(person.name) : person.name == null; } @Override public inthashCode() { int result = id; result = 31 * result + (name ! = null ? name.hashCode() : 0);return result;    }    @Override    public String toString() {        return "Person{" +                "id=" + id +                ", name='" + name + '\''+'}'; }}Copy the code

Write the result of the following code

public class Test {    public static void main(String[] args) {        HashSet set = new HashSet();        Person p1 = new Person(1001,"AA");        Person p2 = new Person(1002,"BB");        set.add(p1);        set.add(p2);        p1.name = "CC";        set.remove(p1);        System.out.println(set);    }}Copy the code

What is the result of the run at this point? You can guess first, and then see the results of the run.

The output is as follows:

Why is this running result?

Because storage p1, is calculated according to the hash value p1 index position, then p1. Modify the name, so when deleting p1, according to p1, now also is the name of the modified p1 hash value calculation of index, calculate the index of the position at this time and storage before p1 index position is not the same, so don’t take p1 deleted.

So if you move on, there’s more.

public class Test {    public static void main(String[] args) {        HashSet set = new HashSet();        Person p1 = new Person(1001,"AA");        Person p2 = new Person(1002,"BB");        set.add(p1);        set.add(p2);        p1.name = "CC";        set.remove(p1);        System.out.println(set);        set.add(new Person(1001,"CC")); Sys, idtem. Out.println (set);    }}Copy the code

What are the results of the run at this point? Can Person(1001, “CC”) be stored successfully? You can guess for yourself, and then see the results.

The result is as follows:

So why is this running result?

P1 =new Person(1001, “AA”); p1=new Person(1001, “AA”) Later, the value of name was changed, so that p1 id=1001,name= “CC”. Id =1001,name=”AA”,name=”AA”, id=1001,name=”AA”,name=”AA” So you can add successfully.

Don’t worry. There’s more to it. Keep reading.

public class Test {    public static void main(String[] args) {        HashSet set = new HashSet();        Person p1 = new Person(1001,"AA");        Person p2 = new Person(1002,"BB");        set.add(p1);        set.add(p2);        p1.name = "CC";        set.remove(p1);        System.out.println(set);        set.add(new Person(1001,"CC"));        System.out.println(set);                set.add(new Person(1001,"AA"));        System.out.println(set);    }}Copy the code

What are the results of the run at this point? When Person(1001, “AA”) is stored again, can it be stored successfully?

The result is as follows:

So why is this running result?

If Person (id=1001,name=”AA”) is hashed to the same position, then false is returned when calling equals (CC).

Note: A Set is an unordered, non-repeatable Set of elements. If you add two identical elements to the same Set, the add operation fails.

Procedure for adding elements to a HashSet:

When storing an element into a HashSet, the HashSet first calculates the hash value of the element, and then sums the hash value with the array length -1 to figure out where the element is stored in the underlying array of the HashSet. Then judge whether there is already an element in this position, if there is no, it will be directly stored successfully. If one or more elements already exist at that location, the elements are compared for equality from front to back. The hash value of the element is compared first. If the hash value is different, the element is added successfully. If the hash values are the same, the equals method of the element’s class is called to compare; If the equals method returns true, they are equal and the add fails; If the equals method returns false, the two are not equal and will be added successfully.