List<Map.Entry<String, Integer>> list = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.sorted((a,b)->b.getValue()-a.getValue())
.limit(3).collect(Collectors.toList());
System.out.printf("\nList:%s:%d", list.get(0).getKey(), list.get(0).getValue());
list.forEach(entry -> System.out.printf("\n%s:%d", entry.getKey(), entry.getValue()));
Optional<Map.Entry<String, Integer>> opt = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(3).findFirst();
System.out.printf("\nOpt:%s:%d", opt.get().getKey(), opt.get().getValue());
Map<String, Integer> map2 = map.entrySet().stream()
.sorted((a,b)->b.getValue()-a.getValue())
.limit(1).collect(Collectors.toMap(m->m.getKey(),m->m.getValue()));
System.out.println("map2:" + map2);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
map.forEach((k, v) -> System.out.println((k + ":" + v)));
map.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
for (Integer value : map.values()) {
System.out.println(value);
}
No comments:
Post a Comment