package stream;

import lombok.Getter;

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

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

        // 使用 Collectors.mapping 和 Collectors.toSet 只收集大于 30 岁的人的年龄
        Set<Integer> agesAbove30 = people.stream()
                .filter(person -> person.getAge() > 30)
                .collect(Collectors.mapping(Person::getAge, Collectors.toSet()));

        System.out.println("Ages above 30: " + agesAbove30);
    }

    @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 + ")";
        }
    }
}