Collector of use

A, Collector's introduction      

1) Collector The foregoing polymerization has been used, a series of operations to return the list after list.stream.

2) Collector introduced by demand: the Green Apple on a list, yellow Apple on a list

Code examples:

 1 package com.cy.java8;
 2 
 3 import java.util.*;
 4 import java.util.stream.Collectors;
 5 
 6 public class CollectorIntroduce {
 7 
 8     public static void main(String[] args) {
 9         List<Apple> list = Arrays.asList(new Apple("green", 150),
10                                             new Apple("yellow", 120),
11                                             new Apple("green", 170),
12                                             new Apple("green", 150),
13                                             new Apple("yellow", 120),
14                                             new Apple("green", 170) );
15 
16         //Collector的聚合作用
17         List<Apple> greenList = list.stream().filter(a -> a.getColor().equals("green")).collect(Collectors.toList());
18         System.out.println(greenList);
19 
20         Map<String, List<Apple>> result1 = groupByNormal(list);
21         System.out.println(result1);
22 
23         Map<String, List<Apple>> result2 = groupByFunction(list);
24         System.out.println(result2);
25 
26         //Collector of groupBy 
27          the Map <String, List <>> result3 the Apple = groupByCollector (list);
 28          System.out.println (result3);
 29      }
 30  
31 is  
32  
33 is      / ** 
34 is       * requirements: a list on the green yellow in a List
 35       * previously written
 36       * / 
37 [      Private  static the Map <String, List <>> groupByNormal the Apple (List <the Apple> Apples) {
 38 is          the Map <String, List <Map the Apple >> = new new the HashMap <> ();
 39  
40          for (the Apple A: Apples) {
 41 is              List <the Apple> List =as map.get (a.getColor ());
 42 is              IF (List == null ) {
 43 is                  List = new new the ArrayList <> ();
 44 is                  map.put (a.getColor (), List);
 45              }
 46 is              List.add (a);
 47          }
 48  
49          return Map;
 50      }
 51 is  
52 is      / ** 
53 is       * demand: the green in a list, in a yellow List
 54 is       * FunctionInterface method using
 55       * While removal of the determination null operation, but it is still a very long-winded, is not enough to streamline the
 56       * / 
57      Private  static Map<String, List<Apple>> groupByFunction(List<Apple> apples){
58         Map<String, List<Apple>> map = new HashMap<>();
59 
60         apples.stream().forEach(a -> {
61             List<Apple> colorList = Optional.ofNullable(map.get(a.getColor())).orElseGet(() -> {
62                 List<Apple> list = new ArrayList<>();
63                 map.put(a.getColor(), list);
64                 return list;
65             });
66             colorList.add(a);
67         });
68 
69          return Map;
 70      }
 71 is  
72      / ** 
73 is       * Demand: The green in a list, in a yellow List
 74       * Use Collector
 75       * / 
76      Private  static the Map <String, List <>> groupByCollector the Apple ( List <the Apple> Apples) {
 77          return apples.stream () the collect (Collectors.groupingBy (the getColor the Apple ::.));
 78      }
 79 }

Print Results:

[Apple(color=green, weight=150), Apple(color=green, weight=170), Apple(color=green, weight=150), Apple(color=green, weight=170)]
{green=[Apple(color=green, weight=150), Apple(color=green, weight=170), Apple(color=green, weight=150), Apple(color=green, weight=170)], yellow=[Apple(color=yellow, weight=120), Apple(color=yellow, weight=120)]}
{green=[Apple(color=green, weight=150), Apple(color=green, weight=170), Apple(color=green, weight=150), Apple(color=green, weight=170)], yellow=[Apple(color=yellow, weight=120), Apple(color=yellow, weight=120)]}
{green=[Apple(color=green, weight=150), Apple(color=green, weight=170), Apple(color=green, weight=150), Apple(color=green, weight=170)], yellow=[Apple(color=yellow, weight=120), Apple(color=yellow, weight=120)]}

 

two,

 

 

 

 

 

 

 

 

-----

 

Guess you like

Origin www.cnblogs.com/tenWood/p/11530945.html