不同的方法應調用具有空列表的 reduce 方法作為標識。如何使用累加器來檢查舊列表的值是否已在新列表中。@Overridepublic <R> R reduce(R identity, BiFunction<R, ? super E, R> accumulator) { for (E value : this) { identity = accumulator.apply(identity, value); } return identity;}@Overridepublic List<E> distinct() { List<E> list = new LinkedList<E>(); return reduce(list, (a, b) -> );}
1 回答

長風秋雁
TA貢獻1757條經驗 獲得超7個贊
您應該使用 來檢查某個元素是否在列表中。如果是,請不要將其添加到累加器中,否則,請添加它。contains
return reduce(list, (acc, element) -> {
if (!acc.contains(element)) {
acc.add(element);
}
return acc;
});
添加回答
舉報
0/150
提交
取消