public Map<String, List<String>> getWordList(List<String> list) { Map<String, List<String>> ret = new HashMap<>(); for (int i = 0; i < list.size(); i++) { String s = list.get(i); char keyAry[] = new char[26];//26个小写的英文字母 for (int j = 0; j < s.length(); j++) { char charItem = s.charAt(j); int index = charItem - 97; keyAry[index] = charItem; } String keyStr = String.valueOf(keyAry); List<String> keyList = ret.get(keyStr); if (keyList == null) { keyList = new ArrayList<>(); ret.put(keyStr, keyList); } keyList.add(s); } return ret; }
public Map<Integer, List<String>> getWordList(List<String> list) { Map<Integer, List<String>> ret = new HashMap<>(); for (int i = 0; i < list.size(); i++) { String s = list.get(i); int key = 0; for (int j = 0; j < s.length(); j++) { char charItem = s.charAt(j); int index = charItem - 'a'; key |= 1 << index; } List<String> keyList = ret.get(key); if (keyList == null) { keyList = new ArrayList<>(); ret.put(key, keyList); } keyList.add(s); } return ret; }