package stream;

import lombok.Getter;

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

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

        // 使用 Collectors.partitioningBy 按年龄分组
        Map<Boolean, List<Person>> partitionedByAge = people.stream()
                .collect(Collectors.partitioningBy(person -> person.getAge() >= 18));

        // 输出结果
        System.out.println("成年人:");
        partitionedByAge.get(true).forEach(person -> System.out.println(person.getName()));

        System.out.println("未成年人:");
        partitionedByAge.get(false).forEach(person -> System.out.println(person.getName()));
    }

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

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