Java 8: how to perform stream reduce and collection operations

Source: Internet
Author: User

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.

 
 
  1. Package lambda;
  2. Import java. util. Arrays;
  3. Import java. util. List;
  4. Import java. util. Map;
  5. Import java. util. stream. Collectors;
  6. Public class LambdaMapReduce {
  7. Private static List <User> users = Arrays. asList (
  8. New User (1, "Zhang San", 12, User. Sex. MALE ),
  9. New User (2, "Li Si", 21, User. Sex. FEMALE ),
  10. New User (3, "Wang Wu", 32, User. Sex. MALE ),
  11. New User (4, "Zhao six", 32, User. Sex. FEMALE ));
  12. Public static void main (String [] args ){
  13. Performanceavg ();
  14. ReduceSum ();
  15. // Unlike the stream. reduce method, Stream. collect modifies the existing value, instead of creating a new value for each element processed.
  16. // Obtain the average age of all male users
  17. Averager averageCollect = users. parallelStream ()
  18. . Filter (p-> p. getGender () = User. Sex. MALE)
  19. . Map (User: getAge)
  20. . Collect (Averager: new, Averager: accept, Averager: combine );
  21. System. out. println ("Average age of male members :"
  22. + AverageCollect. average ());
  23. // Obtain the list of users older than 12
  24. List <User> list = users. parallelStream (). filter (p-> p. age> 12)
  25. . Collect (Collectors. toList ());
  26. System. out. println (list );
  27. // Count the number of users by gender
  28. Map <User. Sex, Integer> map = users. parallelStream (). collect (
  29. Collectors. groupingBy (User: getGender,
  30. Collectors. summingInt (p-> 1 )));
  31. System. out. println (map );
  32. // Obtain the user name by gender
  33. Map <User. Sex, List <String> map2 = users. stream ()
  34. . Collect (
  35. Collectors. groupingBy (
  36. User: getGender,
  37. Collectors. mapping (User: getName,
  38. Collectors. toList ())));
  39. System. out. println (map2 );
  40. // Calculate the total age by gender
  41. Map <User. Sex, Integer> map3 = users. stream (). collect (
  42. Collectors. groupingBy (User: getGender,
  43. Collectors. cing (0, User: getAge, Integer: sum )));
  44. System. out. println (map3 );
  45. // Calculate the average age by gender
  46. Map <User. Sex, Double> map4 = users. stream (). collect (
  47. Collectors. groupingBy (User: getGender,
  48. Collectors. averagingInt (User: getAge )));
  49. System. out. println (map4 );
  50. }
  51. // Note: each time a reduce operation processes an element, a new value is always created,
  52. // Stream. reduce is applicable when a single result value is returned.
  53. // Obtain the average age of all users
  54. Private static void performanceavg (){
  55. // The pipeline of mapToInt can be followed by average, max, min, count, sum
  56. Double avg = users. parallelStream (). mapToInt (User: getAge)
  57. . Average (). getAsDouble ();
  58. System. out. println ("performanceavg User Age:" + avg );
  59. }
  60. // Obtain the total age of all users
  61. Private static void reduceSum (){
  62. Double sum = users. parallelStream (). mapToInt (User: getAge)
  63. . Reduce (0, (x, y)-> x + y); // can be abbreviated as. sum ()
  64. System. out. println ("reduceSum User Age:" + sum );
  65. }
  66. }
   

Iii. References

Http://docs.oracle.com/javase/tutorial/collections/streams/reduction.html

Link: http://my.oschina.net/cloudcoder/blog/215169

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.