package stream;

import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample10 {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 35)
        );

        // 使用 Collectors.collectingAndThen 计算平均年龄并转换为整数
        int averageAge = people.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.averagingInt(Person::getAge),// 先计算平均年龄
                        Double::intValue // 之后将结果转换为整数
                ));

        System.out.println("Average Age: " + averageAge);
    }

    @Getter
    static class Person {
        private final String name;
        private final int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return name + " (" + age + ")";
        }
    }
}