You can abstract that method in a utilities method like:
public boolean allUnique(Object... objs) {
Set<Object> set = new HashSet<Object>();
for (Object o : objs)
set.add(o);
return set.size() == objs.length
}
The method may not perform well for small numbers (due to the overhead of creating the Set
and the varargs array). However, it grows linearly O(n)
, and for large values it's better than the quadratic growth of a nested if
statements.