java LinkedHashSet

I have been studying for ocjp (former scjp), and I have encountered the following example of using linkedhashset:

public class Test{

    int size;

    public Test(int s){
       this.size = s;
    }

    @Override
    public boolean equals(Object obj) {
         return (this.size == ((Test)obj).size);
    }

    public static void main(String[] args) {
      LinkedHashSet<Test> s = new LinkedHashSet<Test>();
      s.add(new Test(1));
      s.add(new Test(2));
      s.add(new Test(1));
      System.out.println(s.size());
    }
}

Now, the problem is that if the contents displayed are: 1) the implementation remains the same; 2) the rewriting of hashcode is inserted into the test class, as shown below:

public int hashCode() {return size/5};

Running and compiling the code shows that in the first case, the size of set is 3, while in the second case, it is 2 Why?

In case 1, although the equals method is overridden, it is never called Does this mean that if the hashcode method is not overridden, the add () method does not check whether objects are equal? In case 2, the hashcode with the given implementation and the given set of test objects always return the same number How is this different from the default hashcode implementation, and why does it cause equals to be called?

Solution

If hashcode () is not overridden, each instance will have a hash code calculated according to some predefined hash algorithms in the object class Therefore, all your instances may have different hash code values (although this is not certain) Means that each instance will enter its own bucket

Now, even if you override the equals () method and make the two instances equal according to some properties, their hash codes are still different

Therefore, two instances with different hash codes will never be equal So the size of the set is 3 Because it has no repetition

However, when you override hashcode () with the following implementation: –

public int hashCode() {return size/5};

It will return the same value of the same size Therefore, instances with the same size value will have the same hash code, and because you have compared them in the equals method according to size, they will be equal, so they will be treated as duplicates in your set and will be deleted So, set Size () is 2

Morality: – whenever you override the equals () method, you should always override hashcode () to maintain the general contract between the two methods

General contract between hashcode and equals methods: –

>When two objects are equal, their hash codes must be equal > when two objects are not equal, their hash codes can be equal > the hashcode algorithm should always generate the same value for the same object. > If the hashcodes of two objects are different, they will not be equal > always use the same attribute to calculate the hashcode used to compare two instances

It is strongly recommended to read at least once: –

> Effective Java - Item#9: Always override hashCode when you override equals

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>