finance-analyzer/src/main/java/de/arminwolf/financeanalyzer/util/StreamUtil.java

30 lines
634 B
Java

package de.arminwolf.financeanalyzer.util;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamUtil<T> {
public static <T> List<T> findDuplicateInStream(List<T> list) {
// Set to store the duplicate elements
Set<T> items = new HashSet<>();
// Return the set of duplicate elements
return list.stream()
// Set.add() returns false
// if the element was
// already present in the set.
// Hence filter such elements
.filter(items::add)
// Collect duplicate elements
// in the set
.collect(Collectors.toList());
}
}