#### Stream排序Map集合

1、map 根据value排序

``````Map<String,BigDecimal> map =new HashMap<>();
map.put("one", 0.08);
map.put("two", 0.1);
map.put("three", 0.2);
map.put("four", 0.91);``````

``````public <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByValue()
.reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}``````

public <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

``````    map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByKey()
.reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}``````

public <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

``````    map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByValue()
).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}``````

map根据value倒序排序

``map.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).forEach(System.out::println);``

map根据key倒序排序

``map.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).forEach(System.out::println);``

map根据value正序排序

``map.entrySet().stream().sorted(Comparator.comparing(e -> e.getValue())).forEach(System.out::println);``

map根据key正序排序

``map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey())).forEach(System.out::println);``