集合中的数据修改之后并不能影响集合中的hashcode的变化

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

class Data
{
   public int x=6;

    public int y=7;

    public Data(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Data data = (Data) o;
        return x == data.x &&
                y == data.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}
public class TestSet {

    public static void main(String[] args) {
        Set<Data> set=new HashSet<>();

        Data data1=new Data(3,4);

        Data data2=new Data(3,5);

        set.add(data1);
        set.add(data2);

        System.out.println(set.size());

        data2.y=4;

        System.out.println(set.size());

    }
}

发布了184 篇原创文章 · 获赞 19 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/zhoumingsong123/article/details/83904435