Skip to content

Instantly share code, notes, and snippets.

@pujansrt
Last active November 21, 2021 20:45
Show Gist options
  • Save pujansrt/5bee4f52da3c748e1c18d18961b4c08d to your computer and use it in GitHub Desktop.
Save pujansrt/5bee4f52da3c748e1c18d18961b4c08d to your computer and use it in GitHub Desktop.
Collections
// Sory by Value
public static LinkedHashMap<String, Integer> sortByValue(Map<String, Integer> map) {
return map.entrySet()
.stream()
.sorted((Map.Entry.<String, Integer>comparingByValue().reversed()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
// Count words in sentence
for (String word : arr) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
map.forEach((k, v) -> System.out.println(k + "=" + v));
for (Map.Entry<?, ?> entry : m.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
Iterator it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
Map<String, Long> map = Arrays.stream(str.split("")).collect(Collectors.groupingBy(c -> c, Collectors.counting()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment