Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @param list The list to be checked for duplicates
- * @param <T>
- * @return A TreeMap with only one item. The key of the Map is a Boolean that indicates whether dublicates exists or not. The value is a HashSet of the duplicate values.
- */
- public static <T> TreeMap<Boolean,Set<T>> findDuplicates(Collection<T> list) {
- Set<T> duplicates = new HashSet<T>();
- Set<T> uniques = new HashSet<T>();
- TreeMap<Boolean,Set<T>> map = new TreeMap();
- for(T t : list) {
- if(!uniques.add(t)) {
- duplicates.add(t);
- }
- }
- Boolean foundDublicates = false;
- if (duplicates.size() > 0){
- foundDublicates = true;
- }
- map.put(foundDublicates,duplicates);
- return map;
- }
Advertisement
Add Comment
Please, Sign In to add comment