相关疑难解决方法(0)

HashSet'add'方法调用何时等于?

我在HashSet比较中做了这个测试而equals 没有被调用

当farAway = false时,我想考虑等于(检查两个点距离的函数)

完全可编译的代码,你可以测试它,并告诉为什么在这个例子中没有调用equals.

public class TestClass{
     static class Posicion
    {
        private int x;
        private int y;

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Posicion other = (Posicion) obj;
            if ( farAway(this.x, other.x, this.y, other.y,5)){   
                return false;
            } 
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7; hash = 59 * hash + this.x; hash = 59 * …
Run Code Online (Sandbox Code Playgroud)

java comparison equals hashset

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

comparison ×1

equals ×1

hashset ×1

java ×1