【发布时间】:2016-07-05 15:46:01
【问题描述】:
我希望我的Set 中只有 2 个元素,但我在打印时收到了 3 个元素!如何定义唯一性?
public class test {
public static void main(String[] args) {
class bin {
int a;
int b;
bin (int a, int b){
this.a=a;
this.b=b;
}
public boolean Equals(bin me) {
if(this.a==me.a && this.b==me.b)
return true;
else
return false;
}
@Override
public String toString() {
return a+" "+b;
}
}
Set<bin> q= new HashSet<bin>();
q.add(new bin(11,23));
q.add(new bin(11,23));
q.add(new bin(44,25));
System.out.println(q);
}
}
【问题讨论】:
-
您在打印之前从未测试过唯一性