Skip to content

Commit d6f9b92

Browse files
authored
Merge pull request eugenp#8160 from alessiostalla/BAEL-18410
#BAEL-18410 add code sample for reduce with complex objects
2 parents 1e90a56 + bb7c57f commit d6f9b92

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.streamreduce.entities;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Rating {
7+
8+
double points;
9+
List<Review> reviews = new ArrayList<>();
10+
11+
public Rating() {}
12+
13+
public void add(Review review) {
14+
reviews.add(review);
15+
computeRating();
16+
}
17+
18+
private double computeRating() {
19+
double totalPoints = reviews.stream().map(Review::getPoints).reduce(0, Integer::sum);
20+
this.points = totalPoints / reviews.size();
21+
return this.points;
22+
}
23+
24+
public static Rating average(Rating r1, Rating r2) {
25+
Rating combined = new Rating();
26+
combined.reviews = new ArrayList<>(r1.reviews);
27+
combined.reviews.addAll(r2.reviews);
28+
combined.computeRating();
29+
return combined;
30+
}
31+
32+
public double getPoints() {
33+
return points;
34+
}
35+
36+
public List<Review> getReviews() {
37+
return reviews;
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.streamreduce.entities;
2+
3+
public class Review {
4+
5+
int points;
6+
String review;
7+
8+
public Review(int points, String review) {
9+
this.points = points;
10+
this.review = review;
11+
}
12+
13+
public int getPoints() {
14+
return points;
15+
}
16+
17+
public void setPoints(int points) {
18+
this.points = points;
19+
}
20+
21+
public String getReview() {
22+
return review;
23+
}
24+
25+
public void setReview(String review) {
26+
this.review = review;
27+
}
28+
}

core-java-modules/core-java-8/src/main/java/com/baeldung/streamreduce/entities/User.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class User {
44

55
private final String name;
66
private final int age;
7+
private final Rating rating = new Rating();
78

89
public User(String name, int age) {
910
this.name = name;
@@ -17,7 +18,11 @@ public String getName() {
1718
public int getAge() {
1819
return age;
1920
}
20-
21+
22+
public Rating getRating() {
23+
return rating;
24+
}
25+
2126
@Override
2227
public String toString() {
2328
return "User{" + "name=" + name + ", age=" + age + '}';

core-java-modules/core-java-8/src/test/java/com/baeldung/streamreduce/tests/StreamReduceManualTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.streamreduce.tests;
22

3+
import com.baeldung.streamreduce.entities.Rating;
4+
import com.baeldung.streamreduce.entities.Review;
35
import com.baeldung.streamreduce.entities.User;
46
import com.baeldung.streamreduce.utilities.NumberUtils;
57
import java.util.ArrayList;
@@ -64,6 +66,31 @@ public void givenUserList_whenReduceWithAgeAccumulatorAndSumCombiner_thenCorrect
6466
assertThat(result).isEqualTo(65);
6567
}
6668

69+
@Test
70+
public void givenUserList_whenReduceWithGreaterAgeAccumulator_thenFindsOldest() {
71+
List<User> users = Arrays.asList(new User("John", 30), new User("Alex", 40), new User("Julie", 35));
72+
73+
User oldest = users.stream().reduce(users.get(0), (user1, user2) -> user1.getAge() >= user2.getAge() ? user1 : user2);
74+
75+
assertThat(oldest).isEqualTo(users.get(1));
76+
}
77+
78+
@Test
79+
public void givenUserListWithRatings_whenReduceWithGreaterAgeAccumulator_thenFindsOldest() {
80+
User john = new User("John", 30);
81+
john.getRating().add(new Review(5, ""));
82+
john.getRating().add(new Review(3, "not bad"));
83+
User julie = new User("Julie", 35);
84+
john.getRating().add(new Review(4, "great!"));
85+
john.getRating().add(new Review(2, "terrible experience"));
86+
john.getRating().add(new Review(4, ""));
87+
List<User> users = Arrays.asList(john, julie);
88+
89+
Rating averageRating = users.stream().reduce(new Rating(), (rating, user) -> Rating.average(rating, user.getRating()), Rating::average);
90+
91+
assertThat(averageRating.getPoints()).isEqualTo(3.6);
92+
}
93+
6794
@Test
6895
public void givenStringList_whenReduceWithParallelStream_thenCorrect() {
6996
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");

parent-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<groupId>com.baeldung</groupId>
1212
<artifactId>parent-modules</artifactId>
1313
<version>1.0.0-SNAPSHOT</version>
14+
<relativePath>../</relativePath>
1415
</parent>
1516

1617
<dependencies>

0 commit comments

Comments
 (0)