Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 4th, 2013  |  syntax: None  |  size: 0.86 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Read words from file and put into a simulated multimap
  2.     Map<String, List<String>> m = new HashMap<String, List<String>>();
  3.  
  4.     try {
  5.         Scanner s = new Scanner(new File(args[0]));
  6.         while (s.hasNext()) {
  7.             String word = s.next();
  8.             String alpha = alphabetize(word);
  9.             List<String> l = m.get(alpha);
  10.             if (l == null)
  11.                 m.put(alpha, l=new ArrayList<String>());
  12.             l.add(word);
  13.         }
  14.     } catch (IOException e) {
  15.         System.err.println(e);
  16.         System.exit(1);
  17.     }
  18.  
  19.     // Print all permutation groups above size threshold
  20.     for (List<String> l : m.values())
  21.         if (l.size() >= minGroupSize)
  22.             System.out.println(l.size() + ": " + l);
  23. }
  24.  
  25. private static String alphabetize(String s) {
  26.     char[] a = s.toCharArray();
  27.     Arrays.sort(a);
  28.     return new String(a);
  29. }