In java 8, what\'s the best way to check if a List contains any duplicate?
My idea was something like:
list.size() != list.stream().distinct().count(
Started this class as a StreamTool, but I think there must be an even better way with reduce or similar:
public class StreamTool {
/**
* Whether stream records are unique in that stream.
* @param Type of records
* @param records
* @return true if there are no duplicates, false otherwise
*/
public static boolean isUnique(Stream records) {
return records.allMatch(new HashSet<>()::add);
}
}