If you are in alphabetical order of letters than a hash, they should be the same ...
Map<String, List<String>> words = new HashMap<String, List<String>>(); for(String word : incomingWords) { final String key = alphabetize(word); if(words.contains(key)){ words.get(key).add(word); } else { words.put(key, new ArrayList<String>()); words.get(key).add(word); } }
Now you have a Map words that are anagrams ... You will also have a List in Map that has only 1 entry, you can remove them from the map to just save Map which have other anagrams from your dictionary ...
source share