Guest User

Untitled

a guest
Dec 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. package java8.streams;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6.  
  7. public class ReduceTest {
  8. static class Person implements Comparable<Person> {
  9. String name;
  10. int age;
  11.  
  12. Person(String name, int age) {
  13. this.name = name;
  14. this.age = age;
  15. }
  16.  
  17. @Override
  18. public String toString() {
  19. return name;
  20. }
  21.  
  22. @Override
  23. public int compareTo(Person other) {
  24. if(this.age == other.age) return 0;
  25. else return ((this.age > other.age) ? 1 : -1);
  26. }
  27. }
  28. public static void main(String[] args) {
  29. List<Person> persons =
  30. Arrays.asList(
  31. new Person("Max", 18),
  32. new Person("Peter", 23),
  33. new Person("Pamela", 23),
  34. new Person("David", 12));
  35.  
  36. // Reduces a stream of elements to exactly one element of the stream
  37. persons
  38. .stream()
  39. .reduce((p1, p2) -> p1.age > p2.age ? p1 : p2)
  40. .ifPresent(System.out::println); // Pamela
  41.  
  42. // method accepts both an identity value and a BinaryOperator accumulator. This method can be utilized to construct a new
  43. Person result =
  44. persons
  45. .stream()
  46. .reduce(new Person("",0), (p1, p2) -> {
  47. p1.age += p2.age;
  48. p1.name += p2.name;
  49. return p1;
  50. });
  51.  
  52. System.out.format("name=%s; age=%s", result.name, result.age);
  53. System.out.println();
  54. System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  55. // The third reduce method accepts three parameters: an identity value, a BiFunction accumulator and a combiner function of type BinaryOperator
  56. Integer ageSum = persons
  57. .stream()
  58. .reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);
  59.  
  60. System.out.println(ageSum); // 76
  61. System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  62. //Third reduce in details
  63. Integer ageSumDtails = persons
  64. .stream()
  65. .reduce(0,
  66. (sum, p) -> {
  67. System.out.format("accumulator: sum=%s; person=%s\n", sum, p);
  68. return sum + p.age;
  69. },
  70. (sum1, sum2) -> {
  71. System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2);
  72. return sum1 + sum2;
  73. });
  74. System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  75. // Combiner getting called in .parallelStream()
  76.  
  77. //Third reduce in details
  78. Integer ageSumParallel = persons
  79. .parallelStream()
  80. .reduce(0,
  81. (sum, p) -> {
  82. System.out.format("accumulator: sum=%s; person=%s\n", sum, p);
  83. return sum + p.age;
  84. },
  85. (sum1, sum2) -> {
  86. System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2);
  87. return sum1 + sum2;
  88. });
  89. }
  90.  
  91. }
Add Comment
Please, Sign In to add comment