java8 stream常用写法
// 实体类
public static class User {
private String id;
private String name;
private String value;
private String year;
}
List list = userMapper.selectAll(param);
List 转 Map
Map idMap = list.stream().collect(Collectors.toMap(x -> x.getId(), y -> y.getName() + "_" + y.getValue(), (e1,e2) -> e1, LinkedHashMap::new));
// 以 id为Map key
Map> idMap = list.stream().collect(Collectors.groupingBy(User::getId));
// 以 id_name 为Map key
Map> idMap = list.stream().collect(Collectors.groupingBy(x -> x.getId + "_" + x.getName));
List list = userMapper.selectAll(param);
// List 转 Map
Map> idMap = list.stream().collect(Collectors.groupingBy(User::getId));
// 排序方式1 Map 根据 id 从小到大排序(降序修改(e1, e2) e1 e2 的位置换一换即可)
Map> 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> sort2IdMap = idMap.entrySet().stream()
.sorted(Map.Entry.>comparingByKey().reversed())
.forEachOrdered(x -> sortMap.put(x.getKey(),x.getValue()));
Map> sort22IdMap = idMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEachOrdered(x -> sortMap.put(x.getKey(),x.getValue()));
//进阶
// Map 根据 另一个 Map 排序
Map initMap = new LinkedHashMap<>();
initMap.put("张三",3);
initMap.put("李四",2);
initMap.put("王五",1);
// initMap 根据value排序
Map m1 = initMap.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue,(e1, e2) -> e1,
LinkedHashMap::new));
// name 作为key的Map
Map> nameMap = list.stream().collect(Collectors.groupingBy(User::getName));
// nameMap 根据 ml的key值(name)进行排序
Map> sortOtherMap = nameMap.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(m1.get(e1.getKey()), m1.get(e2.getKey())))
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new));
// 排序方式1 根据id 排序
List sort2 = xkmlMcDmBos.stream().sorted(Comparator.comparing(User::getId,
Comparator.reverseOrder())).collect(Collectors.toList());
// 排序方式2 根据id 排序
List sort1 = list.stream().sorted((e1, e2) ->
Integer.compare(Integer.parseInt(e1.getId()),
Integer.parseInt(e2.getId()))).collect(Collectors.toList());
// 排序方式3 (List->String 排完序之后 转成为 name 的 List)
List sort2 = xkmlMcDmBos.stream().sorted(Comparator.comparing(User::getId,
Comparator.reverseOrder())).map(User::getName).
collect(Collectors.toList());
// 排序方式4
List
List filterList = list.stream().filter(x -> x.getName().equals("张三"))
.collect(Collectors.toList());;
int total = list.stream().mapToInt(x -> Integer.parseInt(x.getValue())).sum();
// 过滤加求和
int total = list.stream().filter(x -> x.getName().equals("张三"))
.mapToInt(x -> Integer.parseInt(x.getValue())).sum();
// 最大值
int total = list.stream().mapToInt(x -> Integer.parseInt(x.getValue())).max();
// 最小值
int total = list.stream().mapToInt(x -> Integer.parseInt(x.getValue())).mim();
// 平均值
int total = list.stream().mapToInt(x -> Integer.parseInt(x.getValue())).average();