Skip to content

Instantly share code, notes, and snippets.

@kewang
Created May 18, 2015 02:21
Show Gist options
  • Save kewang/ab74e7d0d6a9d8e1c9ff to your computer and use it in GitHub Desktop.
Save kewang/ab74e7d0d6a9d8e1c9ff to your computer and use it in GitHub Desktop.
Map<String, Integer> sources = new HashMap<String, Integer>();
Map<Integer, ArrayList<String>> results = new HashMap<Integer, ArrayList<String>>();
sources.put("a", 1);
sources.put("b", 3);
sources.put("c", 5);
sources.put("d", 1);
sources.put("e", 3);
sources.put("f", 7);
sources.put("g", 7);
sources.put("h", 3);
for (Entry<String, Integer> entry : sources.entrySet()) {
ArrayList<String> arr = results.get(entry.getValue());
if (arr == null) {
arr = new ArrayList<String>();
arr.add(entry.getKey());
results.put(entry.getValue(), arr);
} else {
arr.add(entry.getKey());
}
}
for (Entry<Integer, ArrayList<String>> entry : results.entrySet()) {
System.out.println("key: " + entry.getKey());
System.out.print("values: ");
for (String s : entry.getValue()) {
System.out.print(s + ", ");
}
System.out.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment