Stream sorting Map collection

Recent small series by himself in charge of a project to develop the background, part of which is statistically relevant features and require some sort of operation or packets, before this operation Xiao Bian think it is too much trouble, although there are some ready-made hot tools, but tools of writing is more complicated, but if you use java8 stream flows would be relatively simple, and will greatly reduce the amount of code, the following summarizes several operations on the map.

1, map value according to the ordering

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);

The above intermediate is a result of the project, we need the map value according to the value of reverse sort, tools are given below:

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;
}

Of course, if we want to be sorted according to key map, the need for the above tools were minor changes, as follows:

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;
}

We can see if we need to key sort, you need to make key Comparable inheritance, it says we need to be sorted field is inherited Comparable interface. Another problem is that the above effect of such an approach is sorted in descending order, ascending order if we need it, just to the above .reversed () keyword restrictions can be removed.

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;
}

Sort Descending map value

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

 

The descending sort key map

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

 

Sort positive sequence value map

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

 

Sort key map positive sequence

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

reference

Original link: https://blog.csdn.net/hao134838/article/details/80780622
description link: https://blog.csdn.net/qq_41011894/article/details/88405944

Guess you like

Origin www.cnblogs.com/eternityz/p/12238979.html