下面的代码是不是给我,我期待的结果:问题与“包含” HashSet的方法(JAVA)
public static void main (String[] args) {
Set<Pair> objPair = new LinkedHashSet<Pair>();
objPair.add(new Pair(1, 0));
System.out.println("Does the pair (1, 0) exists already? "+objPair.contains(new Pair(1, 0)));
}
private static class Pair {
private int source;
private int target;
public Pair(int source, int target) {
this.source = source;
this.target = target;
}
}
结果将是:
Does the pair (1, 0) exists already? false
我不能理解为什么它不起作用。 或者,也许我正在使用“包含”方法错误(或错误的原因)。
也有另一种问题, 如果我添加相同值的两倍,它会被接受,甚至是一套
objPair.add(new Pair(1, 0));
objPair.add(new Pair(1, 0));
它不会接受/识别类对我产生的?
在此先感谢。
'HashSet'可与散列码哪里是你的'的hashCode() '执行? –
哦,是的,我是新来的,我可能需要更多地研究它:) 非常感谢你! –