【发布时间】:2021-05-12 11:13:03
【问题描述】:
考虑这段代码
String[] colours1 = new String[]{"red", "green", "blue"};
String[] colours2 = new String[]{"red", "yellow", "blue"};
String[] colours3 = new String[]{"red", "green", "blue"};
List<String[]> distinct = Stream.of(colours1, colours2, colours3)
.distinct() // Do something here to compare arrays better
.collect(Collectors.toList());
我希望 distinct 列表仅包含 2 个元素 colours1 和 colours2(因为 1 和 3 是等价的)。但是,由于流 distinct() 方法执行等于比较,它仍然包含所有 3 种颜色数组。我想要一个自定义的独特功能,您可以在其中提供比较器。在这种情况下,Objects#deepEquals 就足够了。有没有简单的方法来实现这一点?
【问题讨论】:
标签: java java-stream