stream常用写法
// 实体类
public static class User {private String id;private String name;private String value;private String year;
}
1.List转Map
List<User> list = userMapper.selectAll(param);
List 转 MapMap<String, String> idMap = list.stream().collect(Collectors.toMap(x -> x.getId(), y -> y.getName() + "_" + y.getValue(), (e1,e2) -> e1, LinkedHashMap::new));// 以 id为Map key
Map<String, List<User>> idMap = list.stream().collect(Collectors.groupingBy(User::getId));// 以 id_name 为Map key
Map<String, List<User>> idMap = list.stream().collect(Collectors.groupingBy(x -> x.getId + "_" + x.getName));
2.Map排序
List<User> list = userMapper.selectAll(param);
// List 转 Map
Map<String, List<User>> idMap = list.stream().collect(Collectors.groupingBy(User::getId));
// 排序方式1 Map 根据 id 从小到大排序(降序修改(e1, e2) e1 e2 的位置换一换即可)
Map<String, List<User>> sort1IdMap = idMap.entrySet().stream().sorted((e1, e2) ->Integer.compare(Integer.parseInt(e1.getKey()), Integer.parseInt(e2.getKey()))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));// 排序方式2
Map<String, List<User>> sort2IdMap = idMap.entrySet().stream().sorted(Map.Entry.<String,List<User>>comparingByKey().reversed()).forEachOrdered(x -> sortMap.put(x.getKey(),x.getValue()));Map<String, List<User>> sort22IdMap = idMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> sortMap.put(x.getKey(),x.getValue()));//进阶// Map 根据 另一个 Map 排序
Map<String, Integer> initMap = new LinkedHashMap<>();
initMap.put("张三",3);
initMap.put("李四",2);
initMap.put("王五",1);// initMap 根据value排序
Map<Str