如何使自定义Tuple类通用?

问题描述 投票:0回答:1

我正在尝试创建一个通用的元组类。它将其元素存储为ArrayList。当然,这个类应该覆盖hashcode和equals方法。

我怎么能为这个类制作哈希码方法?你看,在代码中,我遇到了麻烦。

另外,对于equals方法,为什么编译器强迫我使用'?'。为什么我不能只使用T?

public static class Tuple<T> { 
        ArrayList<T> tuple = new ArrayList<>();

        public Tuple(ArrayList<T> items) {
            for (T item : items) {
                tuple.add(item);
            }
        }

        @Override
        public int hashCode() {
            T sum = ???;
            for (T item : tuple) {
                sum += item.hashCode();
            }
            return sum;
        }

        @Override 
        public boolean equals(Object o) {
            if (o instanceof Tuple<?>) {
                Tuple<?> tup= (Tuple<?>) o;
                if (tup.tuple.size() != this.tuple.size()) {
                    return false;
                }
                for (int i = 0; i < this.tuple.size(); i++) {
                    if (this.tuple.get(i) != tup.tuple.get(i)) {
                        return false;
                    } 
                }
                return true;
            } else {
                return false;
            }
        }
    }
java generics hashcode
1个回答
0
投票

正如评论中所提到的,我们应该将hashCodeequals方法委托给ArrayList<T> tuple实例变量。对于hashCode来说,它是微不足道的。对于equals而言,它只是稍微复杂一点,因为我们不希望我们的自定义TupleArrayList相等。所以这里是:

public class Tuple<T> {

     // I made this private because I'm pedantric ;)
     private final ArrayList<T> tuple = new ArrayList<>();

     // this does the same as your code, it's just easier to read
     public Tuple(ArrayList<T> items) {
         tuple.addAll(items);
     }

    @Override
    public int hashCode() {
        return tuple.hashCode();
    }

    // generated by eclipse
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Tuple other = (Tuple) obj;
        if (tuple == null) {
            if (other.tuple != null)
                return false;
        } else if (!tuple.equals(other.tuple))
            return false;
        return true;
    }
}

如果你想处理tuple可以为null的情况,那么你可以使用稍微复杂的hashCode

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((tuple == null) ? 0 : tuple.hashCode());
    return tuple.hashCode();
}

一般来说,我不喜欢自己编写这些方法。通常,我让我的IDE生成的东西。我需要注意的是在添加新字段时重新生成它。 Apache HashCodeBuilderEqualsBuilder也是不错的选择。

© www.soinside.com 2019 - 2024. All rights reserved.