Group list of objects by property and calculate average of property of object per key

Robin-Hoodie :

Let's say I have a Person class:

class Person {
    String firstName;
    String age;

    public String getFirstName() {
        return firstName;
    }

    public String getAge() {
        return age;
    }
}

I have a List<Person> as input. As output I'd like to get a DoubleStream of the average age per firstName. I've currently implemented it like this, which works:

public DoubleStream getAverageAgesPerFirstName(List<Person> people) {
    return people
            .stream()
            .collect(
                    groupingBy(Person::getFirstName)
            )
            .entrySet()
            .stream()
            .collect(
                    toMap(Entry::getKey, this::getAverageAge)
            )
            .values()
            .stream()
            .mapToDouble(Double::doubleValue)
}

private double getAverageAge(Entry<String, List<Person>> entry) {
        return entry.getValue().stream().map(Person::getAge).mapToDouble(Integer::doubleValue).average().orElse(0);
    }

I do feel like there should be a more efficient way to go about this. Most notably I'm creating two intermediate maps before converting those to a stream again.

Ousmane D. :

Use a downstream collector to apply further operations upon the grouping, example:

return people.stream()
             .collect(Collectors.collectingAndThen(
                 Collectors.groupingBy(Person::getFirstName, 
                    Collectors.averagingDouble(Person::getAge)), 
                        v -> v.values().stream().mapToDouble(Double::doubleValue));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75357&siteId=1