lamda表达式例子全集详解
1、List 转 map
1.1、key(Model属性) value Model
Map<String, Model> modeMap = List<model>.stream().collect(Collectors.toMap(Model1::属性get方法, v -> v, (p1, p2) -> p1));
1.2、key(Model1属性) value new Model1
Map<String, Model1> model2Map = List<Model1>.stream().collect(Collectors.toMap(Model1::属性get方法, s -> new Model2(s.get属性1, s.get属性2), (v1, v2) -> v1));
1.3、groupBy-key(属性1) value:List(属性2)
Map<String, List<String>> map = List<Model>.stream().collect(groupingBy((Model1::属性1get方法,Collectors.mapping((model::属性2get方法, Collectors.toList())));
1.4、groupBy-key value:Set 空key初始化
Set dimValSet = res.computeIfAbsent(model.get属性一, k -> new HashSet<>());
dimValSet.add(xxxx);
1.5、groupBy-key(属性1) value:Set(方法生成)
Map<String, Set<String>> map = List<Model>.stream().collect(Collectors.toMap(Model1::get属性一, this::单独的方法, (v1, v2) -> v1));
2、list 转 list
2.1、list(model1)转list(model2)
List<Model1> dimGroupAuthVos = List<Model1>.stream().map(e -> new Model2(model1.属性1(),model1.属性2(),model1.属性3())).collect(Collectors.toList());
2.2、list(model1)转list(属性) 并去重
List<Long> 属性数组 = List<Model1>.stream().map(user -> user.getId()).distinct().collect(Collectors.toList());
3、map 转 map
3.1、Map(key,model)转map(key,属性)
Map<String, String> dimEnameMap = map.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().get属性(), (v1, v2) -> v1));