【问题标题】:HashSet does not realize that two objects are the sameHashSet 没有意识到两个对象是相同的
【发布时间】:2019-05-06 22:03:06
【问题描述】:

我看过其他时候在 StackOverflow 上提出过这个问题,但其他用例似乎都没有解决我的问题。 HashSet does not seem to realize that two objects are the same.

基本上,这是我的课。

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));
        }
    }

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

按照我定义我的 equals 方法的方式,不一致 A (10, 20, true) 应该等于另一个不一致 B (20, 10, true),当我测试我的 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

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

【问题讨论】:

    标签: java hashset


    【解决方案1】:

    您对equals 的定义意味着两个元素颠倒的Inconsistency 对象是.equals,但是如果a,您对hashCode 的定义不会返回相同的哈希码和b 的顺序不同,这是HashSet 或其他基于哈希的集合正常工作的要求。

    解决此问题的最简单方法是做一些可交换的事情——无论ab 的顺序如何,结果都相同。例如:

    result = prime * result + a + b;
    

    而不是

    result = prime * result + a;
    result = prime * result + b;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 2017-11-09
      相关资源
      最近更新 更多