Last active
November 21, 2021 20:45
-
-
Save pujansrt/5bee4f52da3c748e1c18d18961b4c08d to your computer and use it in GitHub Desktop.
Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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