Java 8: how to perform stream reduce and collection operations
The Java 8 JDK contains many aggregate operations such as average, sum, minimum, maximum, and count. The aggregation result of a stream is returned. These aggregate operations are called aggregate operations. In addition to the aggregation operation that returns a single value, JDK also returns a collection instance for many aggregation operations. Many reduce operations perform specific tasks, such as averaging or grouping by category.
General aggregation operations provided by JDK: Stream. reduce, Stream. collection
Note: In this document, function operations are translated as aggregation operations because function operations are usually used for aggregation statistics.
Differences between the two:
Stream. reduce. Common methods include average, sum, min, max, and count. Return a single result value. Each time a reduce operation processes an element, a new value is always created.
Stream. collection is different from stream. reduce. Stream. collect modifies the existing value, instead of creating a new value for each element processed.
- Package lambda;
- Import java. util. Arrays;
- Import java. util. List;
- Import java. util. Map;
- Import java. util. stream. Collectors;
- Public class LambdaMapReduce {
- Private static List <User> users = Arrays. asList (
- New User (1, "Zhang San", 12, User. Sex. MALE ),
- New User (2, "Li Si", 21, User. Sex. FEMALE ),
- New User (3, "Wang Wu", 32, User. Sex. MALE ),
- New User (4, "Zhao six", 32, User. Sex. FEMALE ));
- Public static void main (String [] args ){
- Performanceavg ();
- ReduceSum ();
- // Unlike the stream. reduce method, Stream. collect modifies the existing value, instead of creating a new value for each element processed.
- // Obtain the average age of all male users
- Averager averageCollect = users. parallelStream ()
- . Filter (p-> p. getGender () = User. Sex. MALE)
- . Map (User: getAge)
- . Collect (Averager: new, Averager: accept, Averager: combine );
- System. out. println ("Average age of male members :"
- + AverageCollect. average ());
- // Obtain the list of users older than 12
- List <User> list = users. parallelStream (). filter (p-> p. age> 12)
- . Collect (Collectors. toList ());
- System. out. println (list );
- // Count the number of users by gender
- Map <User. Sex, Integer> map = users. parallelStream (). collect (
- Collectors. groupingBy (User: getGender,
- Collectors. summingInt (p-> 1 )));
- System. out. println (map );
- // Obtain the user name by gender
- Map <User. Sex, List <String> map2 = users. stream ()
- . Collect (
- Collectors. groupingBy (
- User: getGender,
- Collectors. mapping (User: getName,
- Collectors. toList ())));
- System. out. println (map2 );
- // Calculate the total age by gender
- Map <User. Sex, Integer> map3 = users. stream (). collect (
- Collectors. groupingBy (User: getGender,
- Collectors. cing (0, User: getAge, Integer: sum )));
- System. out. println (map3 );
- // Calculate the average age by gender
- Map <User. Sex, Double> map4 = users. stream (). collect (
- Collectors. groupingBy (User: getGender,
- Collectors. averagingInt (User: getAge )));
- System. out. println (map4 );
- }
- // Note: each time a reduce operation processes an element, a new value is always created,
- // Stream. reduce is applicable when a single result value is returned.
- // Obtain the average age of all users
- Private static void performanceavg (){
- // The pipeline of mapToInt can be followed by average, max, min, count, sum
- Double avg = users. parallelStream (). mapToInt (User: getAge)
- . Average (). getAsDouble ();
- System. out. println ("performanceavg User Age:" + avg );
- }
- // Obtain the total age of all users
- Private static void reduceSum (){
- Double sum = users. parallelStream (). mapToInt (User: getAge)
- . Reduce (0, (x, y)-> x + y); // can be abbreviated as. sum ()
- System. out. println ("reduceSum User Age:" + sum );
- }
- }
Iii. References
Http://docs.oracle.com/javase/tutorial/collections/streams/reduction.html
Link: http://my.oschina.net/cloudcoder/blog/215169