HashSet没有意识到两个对象是相同的

Kay*_*ylo 1 java hashset

我看过其他时候在StackOverflow上问过这个问题,但是其他用例似乎都不能解决我的问题。HashSet似乎没有意识到两个对象是相同的。

基本上,这是我的课程。

private static class Inconsistency
    {
        int a;
        int b;
        boolean isConsistency;


        //Default constructor. To be used when we don't have an inconsistency
        public Inconsistency()
        {
            this.a = -1;
            this.b = -1;
            this.isConsistency = false;
        }

        public Inconsistency(int a, int b, boolean isConsistency)
        {
            this.a = a;
            this.b = b;
            this.isConsistency = isConsistency;
        }

        @Override
        public String toString()
        {
            if (this.isConsistency) 
            {
                return "(" + this.a + ", " + this.b + ")";
            } 
            else 
            {
                return "No inconsistency";
            }

        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + a;
            result = prime * result + b;
            result = prime * result + (isConsistency ? 1231 : 1237);
            return result;
        }

        @Override
        public boolean equals(Object other)
        {

            if (this == other)
            {
                return true;
            }
            if (other == null)
            {
                return false;
            }

            if (this.getClass() != other.getClass()) 
            { 
                return false; 
            }

            Inconsistency otherInconsistency = (Inconsistency) other;
            return ((this.a == otherInconsistency.a) && (this.b == otherInconsistency.b) && (this.isConsistency == otherInconsistency.isConsistency))
                || ((this.a == otherInconsistency.b) && (this.b == otherInconsistency.a) && (this.isConsistency == otherInconsistency.isConsistency));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我试图将我的类的对象存储在哈希图中。

用我定义自己的equals方法的方式,不一致A(10、20,是)应该等于另一个不一致B(20、10,是),并且当我测试我的equals方法时,这可以正常工作。但是,当我尝试将A和B都插入到HashSet中时,它们都会被错误地添加。我知道我应该操纵我的哈希码功能,但是我不确定该怎么做。

这是显示错误行为的驱动程序

    Inconsistency A = new Inconsistency(10,20, true);
    Inconsistency B = new Inconsistency(20,10, true);

    System.out.println(A.equals(B)); // prints true as expected


    HashSet<Inconsistency> test = new HashSet<>();
    test.add(A);
    test.add(B);

    System.out.println(test); // prints [(10, 20), (20, 10)]. The two objects are equal but are both added to hashset
Run Code Online (Sandbox Code Playgroud)

如此,问题就很清楚了:如何确保两个相等的对象A和B都不会添加到我的HashSet中?

Lou*_*man 6

你的定义,equals指的是两个Inconsistency与扭转它们的元素对象.equals,但你的定义hashCode不会返回相同的散列码是否ab在不同的顺序,这是一个要求,如果一个HashSet或其它基于散列的集合是正常工作。

解决此问题的最简单方法是进行可交换的操作-不管顺序ab所处的顺序都具有相同的结果。例如:

result = prime * result + a + b;
Run Code Online (Sandbox Code Playgroud)

代替

result = prime * result + a;
result = prime * result + b;
Run Code Online (Sandbox Code Playgroud)