JAVA 8之后添加了lambda表达式一直没有系统学习
先记录一下map 利用lambda表达式排序
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.Map.Entry.comparingByKey;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;
public class TestMap {
public static void main(String[] args) {
Map<String ,Integer> maps = new HashMap<>();
maps.put("wzn",30);
maps.put("xhf",29);
maps.put("as", 1);
maps.put("bj",21);
System.out.println("未排序数据:" +maps.toString());
LinkedHashMap collect1 = maps.entrySet().stream().sorted(comparingByValue()).collect(toMap(Map.Entry::getKey,Map.Entry::getValue, (e1, e2) -> e2, LinkedHash