【发布时间】:2018-04-27 15:31:19
【问题描述】:
我有这门课:
class A{
int count=0;
//equals and hashCode implemented
}
创建两个集合:
Collection<A> collection1;//something
Collection<A> collection2;//something
连接集合:
Stream.concat(collection1.stream(), collection2.stream())
//do something here???
.distinct()
.sorted(new Comparator<A>() {
@Override
public int compare(A o1, A o2) {
return new Integer(o1.count).compareTo(new Integer(o2.count));
}
})
.collect(Collectors.toList());
我想合并这两个列表,如果有重复,我想更新整数count; 上面的代码完全摆脱了重复
例如,
假设我们在 collection1 和 collection2 中都有一个对象,它们相等 (o1.equals(o2) == true) 。一个有count=10,另一个有count=20 我想拥有一组不同的对象,该对象有count=30
有办法吗?
【问题讨论】:
-
我不知道如何更清楚地解释它,你能给我一个建议,以便我可以编辑问题,thx
-
您是否在比较计数并在匹配时增加计数?我想也许除了计数之外你还需要一些东西来区分你的类。
-
@BillK 说我们得到了 dups,一个 count=10,一个 count=20。我想保留一个count=30!希望这是有道理的伴侣
-
问题是,你如何识别“那个对象”?看起来你正在通过计数来识别你的对象——我通过你的比较定义来直觉,所以 count=10 和 count=20 的两个将不匹配,并且它们不会被添加到 30,对吧?
标签: java collections java-8