この2つのチェックは、「Listの要素が全て同じ」=true なら、
「Listの要素が全て同じ」= true でなければならない。
List の要素が全て同じ値かどうか
List<T> list;
boolean res = list.isEmpty() || list.stream().allMatch(e->Objects.equals(list.get(0), e));;
過去、重複存在すれば true を返すCollectors
で書いた、Stream の collect( ) で指定する Collector<T,?,Boolean> で boolean 結果、true を期待する方法、
public static<T> Collector<T,?,Boolean> duplicatedElements(){ Set<T> set = new HashSet<>(); return Collectors.reducing(true, set::add, Boolean::logicalXor); }
これは、「Listの要素が全て同じ」の時に、true を返さない
この duplicatedElements() の方法は良さそうに見えるけど、これは、全て同一の時は true にならない。
Stream の collect( ) で指定するなら、
全てユニークか?の裏返しのチェックでないとダメだ。
それは、
リスト要素の重複チェック - Oboe吹きプログラマの黙示録
で書いた以下を使用するとfalse にはなるが、だからと言って、「Listの要素が全て同じ」というわけではない
static<T> Collector<T,?,Boolean> uniqueElements(){ Set<T> set = new HashSet<>(); return Collectors.reducing(true, set::add, Boolean::logicalAnd); }