if (Collections.frequency(list, list.get(0)) == list.size()) { }
... for a list that is not empty. Otherwise,
final int n = list.size(); if (n == 0 || Collections.frequency(list, list.get(0)) == n) { }
See the Collections.frequency specification. It will be cheaper than your approach, as well as others, for example. Collections.nCopies(list.get(0), list.size()).equals(list)
source share