import java.util.*; import java.io.*; public class Anagrams2 { public static void main(String[] args) { int minGroupSize = Integer.parseInt(args[1]); // Read words from file and put into simulated multimap Map> m = new HashMap>(); try { Scanner s = new Scanner(new File(args[0])); while (s.hasNext()) { String word = s.next(); String alpha = alphabetize(word); List l = m.get(alpha); if (l == null) m.put(alpha, l=new ArrayList()); l.add(word); } } catch (IOException e) { System.err.println(e); System.exit(1); } // Make a List of all permutation groups above size threshold List> winners = new ArrayList>(); for (List l : m.values()) if (l.size() >= minGroupSize) winners.add(l); // Sort permutation groups according to size Collections.sort(winners, new Comparator>() { public int compare(List o1, List o2) { return o2.size() - o1.size(); }}); // Print permutation groups for (List l : winners ) { System.out.println(l.size() + ": " + l); } } private static String alphabetize(String s) { char[] a = s.toCharArray(); Arrays.sort(a); return new String(a); } }