我正在尝试在 java 中为整数元组创建一个 Set 。
例如:
class Tuple
{
int first;
int second;
public Tuple(int i, int j)
{
this.first=i;
this.second=j;
}
}
Run Code Online (Sandbox Code Playgroud)
然后尝试填充这样的集合:
Set pairs = new HashSet<Tuple>();
pairs.add(new Tuple(1,2));
pairs.add(new Tuple(1,2));
pairs.add(new Tuple(1,2));
Run Code Online (Sandbox Code Playgroud)
对于多个元组对象。但我仍然通过以下方式得到重复:
System.out.println("Size: " + pairs.size());
for (Tuple t : (HashSet<Tuple>) pairs) {
System.out.println(t.toString());
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助删除重复项吗?