是否有任何Java内置方法可以将2个int的组合组合起来?

是否有任何Java内置方法可以将2个int的组合组合起来?,java,arraylist,hashset,Java,Arraylist,Hashset,这是我的代码: private class UniqueClassByTwoIntProperties { private final int propertyOne; private final int propertyTwo; UniqueCase(final int propertyOne, final int propertyTwo) { this.propertyOne= propertyOne; this.propertyTw

这是我的代码:

private class UniqueClassByTwoIntProperties {
    private final int propertyOne;
    private final int propertyTwo;

    UniqueCase(final int propertyOne, final int propertyTwo) {
        this.propertyOne= propertyOne;
        this.propertyTwo= propertyTwo;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        if (!(obj instanceof UniqueCase)) {
            return false;
        }

        UniqueClassByTwoIntProperties unique = (UniqueClassByTwoIntProperties) obj;

        return unique.claimApplicationId == claimApplicationId && unique.claimCoverageId == claimCoverageId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(propertyOne, propertyTwo);
    }
}
我在一个对象列表中循环,我想通过以下方式获得唯一的:

myList.stream()
      .map(row -> new UniqueClassByTwoIntProperties(row.getOne(), row.getTwo()))
      .collect(Collectors.toSet());

我想知道Java中是否有内置类/方法。我查过字典和MultiMapValues,但有点粗糙

您只需按以下方式操作即可:

Set<UniqueClassByTwoIntProperties> uniques = new HashSet<>(myList);

返回unique.hashCode==hashCode;什么不这当然是非常糟糕的,hashCode的变量空间是int-two-int,变量空间是long。这就是hashCode的字面意义——两个不相等的项可能具有相同的hashCode,但两个相等的项必须具有相同的hashCode。请,请读。你的代码被彻底破坏了。我的列表是什么类型的?@BoristheSpider-Woops,我对这个错误感到尴尬。“我希望我现在能正确地实现它,除了打破的等号,这将导致Set表现得非常奇怪。”Boristeider同意,这就是为什么我要把它放在旁边的原因。虽然回答的重点是实际问题。另一方面,在OPs示例中,他们使用的是流-您能提到Collectors.toSet和distinct吗+1或者way@BoristheSpider收集toSet时不需要distinct,因此使用streams提出的当前解决方案是正确的。如果OP想要一个独特项目的列表,那么distinct就可以了。否则,我就不干了。您的答案仅包括在创建列表后将列表复制到集合。答案并不能完全避免OPs问题,因为OP需要创建UniqueClassByTwoIntProperties——这似乎是为了这个特殊的唯一性约束而覆盖相等的一种方法。
List<UniqueClassByTwoIntProperties> uniques = myList.stream()
                                 .distinct() // for unique objects
                                 .collect(Collectors.toList());