Detailed explanation of the complete set of lamda_expression examples

1. List to map

1.1, key (Model attribute) value Model

Map<String, Model> modeMap = List.stream().collect(Collectors.toMap(Model1::attribute get methods, v -> v, (p1, p2) -> p1));

1.2, key (Model1 attribute) value Model2

Map<String, Model1> model2Map = List.stream().collect(Collectors.toMap(Model1::property get method, s -> new Model2(s.get property 1, s.get property 2), (v1, v2 ) -> v1));

1.3, groupBy-key (attribute 1) value: List (attribute 2)

Map<String, List> map = List.stream().collect(groupingBy((Model1:: attribute 1 get method, Collectors.mapping((model:: attribute 2 get method e, Collectors.toList())));

1.4, groupBy-key (attribute 1) value: Set (code generation)

Set dimValSet = res.computeIfAbsent(model.get属性一, k -> new HashSet<>());
dimValSet.add(xxxx);

1.5, groupBy-key (attribute 1) value: Set (method generation)

Map<String, Set> map = List.stream().collect(Collectors.toMap(Model1::get property one, this::separate method, (v1, v2) -> v1));

2. List to list

2.1, list(model1) to list(model2)

List dimGroupAuthVos = List.stream().map(e -> new Model2(model1.Attribute 1(), model1.Attribute 2(), model1.Attribute 3())).collect(Collectors.toList());

2.2, list (model1) to list (attribute) and deduplication

List attribute array = List.stream().map(user -> user.getId()).distinct().collect(Collectors.toList());

3. Map to map

3.1, Map(key, model) to map(key, attribute)

Map<String, String> dimEnameMap = map.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().get属性(), (v1, v2) -> v1));

Guess you like

Origin blog.csdn.net/chuige2013/article/details/129343641