diakogiannis

Generic Find Duplicates - java

Apr 25th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. /**
  2.      *
  3.      * @param list The list to be checked for duplicates
  4.      * @param <T>
  5.      * @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.
  6.      */
  7.     public static <T> TreeMap<Boolean,Set<T>> findDuplicates(Collection<T> list) {
  8.  
  9.         Set<T> duplicates = new HashSet<T>();
  10.         Set<T> uniques = new HashSet<T>();
  11.         TreeMap<Boolean,Set<T>> map = new TreeMap();
  12.  
  13.         for(T t : list) {
  14.             if(!uniques.add(t)) {
  15.                 duplicates.add(t);
  16.             }
  17.         }
  18.         Boolean foundDublicates = false;
  19.         if (duplicates.size() > 0){
  20.             foundDublicates = true;
  21.         }
  22.         map.put(foundDublicates,duplicates);
  23.         return map;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment