, . IDE, , , Comparator#comparing, :
Map<String, Integer> map = new LinkedHashMap<>();
map.put("garden", 2);
map.put("road", 4);
map.put("street", 5);
map.put("park", 5);
map.put("highway", 5);
map = map.entrySet()
.stream()
.sorted(Comparator.<Entry<String, Integer>, Integer>comparing(Entry::getValue)
.thenComparing(Comparator.comparing(Entry::getKey)))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (k, v) -> {
throw new IllegalStateException(String.format("Duplicate Key: %s", k));
}, LinkedHashMap::new));
System.out.println(map);
:
{ = 2, = 4, = 5, = 5, = 5}
, , , , . ( ):
map = map.entrySet()
.stream()
.sorted(Comparator.<Entry<String, Integer>, Integer>comparing(Map.Entry::getValue).reversed()
.thenComparing(Comparator.comparing(Entry::getKey)))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (k, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", k));
}, LinkedHashMap::new));
: Entry java.util.Map.Entry Collectors java.util.stream.Collectors.