リストから重複要素を抽出する。 - Oboe吹きプログラマの黙示録
を書いたので、
自然に次は、リストからユニークな要素=重複していない要素を抽出したリストを
Collector として生成するのは、以下になります。
public static <T> Collector<T, ?, List<T>> uniquedList(){ Map<T, Integer> map = new HashMap<>(); return Collectors.reducing(new ArrayList<T>() ,t->{ map.put(t, Optional.ofNullable(map.get(t)).map(i->i+1).orElse(1)); return Arrays.asList(t); },(n1, n2)->{ return map.entrySet().stream().filter(e->e.getValue() == 1) .map(e->e.getKey()) .collect(Collectors.toList()); }); }