You can map
from A
--> String
, apply the distinct
intermediate operation, utilise limit(2)
to enable optimisation where possible and then check if count
is less than or equal to 1
in which case all objects have the same name and if not then they do not all have the same name.
boolean result = myList.stream()
.map(A::getName)
.distinct()
.limit(2)
.count() <= 1;
With the example shown above, we leverage the limit(2)
operation so that we stop as soon as we find two distinct object names.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…