Merge pull request #8160 from alessiostalla/BAEL-18410

#BAEL-18410 add code sample for reduce with complex objects
This commit is contained in:
Josh Cummings 2019-11-28 11:21:37 -07:00 committed by GitHub
commit c865567b71
5 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.streamreduce.entities;
import java.util.ArrayList;
import java.util.List;
public class Rating {
double points;
List<Review> reviews = new ArrayList<>();
public Rating() {}
public void add(Review review) {
reviews.add(review);
computeRating();
}
private double computeRating() {
double totalPoints = reviews.stream().map(Review::getPoints).reduce(0, Integer::sum);
this.points = totalPoints / reviews.size();
return this.points;
}
public static Rating average(Rating r1, Rating r2) {
Rating combined = new Rating();
combined.reviews = new ArrayList<>(r1.reviews);
combined.reviews.addAll(r2.reviews);
combined.computeRating();
return combined;
}
public double getPoints() {
return points;
}
public List<Review> getReviews() {
return reviews;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.streamreduce.entities;
public class Review {
int points;
String review;
public Review(int points, String review) {
this.points = points;
this.review = review;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
}

View File

@ -4,6 +4,7 @@ public class User {
private final String name;
private final int age;
private final Rating rating = new Rating();
public User(String name, int age) {
this.name = name;
@ -17,7 +18,11 @@ public class User {
public int getAge() {
return age;
}
public Rating getRating() {
return rating;
}
@Override
public String toString() {
return "User{" + "name=" + name + ", age=" + age + '}';

View File

@ -1,5 +1,7 @@
package com.baeldung.streamreduce.tests;
import com.baeldung.streamreduce.entities.Rating;
import com.baeldung.streamreduce.entities.Review;
import com.baeldung.streamreduce.entities.User;
import com.baeldung.streamreduce.utilities.NumberUtils;
import java.util.ArrayList;
@ -64,6 +66,31 @@ public class StreamReduceManualTest {
assertThat(result).isEqualTo(65);
}
@Test
public void givenUserList_whenReduceWithGreaterAgeAccumulator_thenFindsOldest() {
List<User> users = Arrays.asList(new User("John", 30), new User("Alex", 40), new User("Julie", 35));
User oldest = users.stream().reduce(users.get(0), (user1, user2) -> user1.getAge() >= user2.getAge() ? user1 : user2);
assertThat(oldest).isEqualTo(users.get(1));
}
@Test
public void givenUserListWithRatings_whenReduceWithGreaterAgeAccumulator_thenFindsOldest() {
User john = new User("John", 30);
john.getRating().add(new Review(5, ""));
john.getRating().add(new Review(3, "not bad"));
User julie = new User("Julie", 35);
john.getRating().add(new Review(4, "great!"));
john.getRating().add(new Review(2, "terrible experience"));
john.getRating().add(new Review(4, ""));
List<User> users = Arrays.asList(john, julie);
Rating averageRating = users.stream().reduce(new Rating(), (rating, user) -> Rating.average(rating, user.getRating()), Rating::average);
assertThat(averageRating.getPoints()).isEqualTo(3.6);
}
@Test
public void givenStringList_whenReduceWithParallelStream_thenCorrect() {
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");

View File

@ -11,6 +11,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<dependencies>