import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamsAndMapsAndSystemOuts2 {
    public static void main(String[] args) {
        List<Person> persons =
                Arrays.asList(
                        new Person("Max", 18),
                        new Person("Peter", 23),
                        new Person("Pamela",23),
                        new Person("David", 12)
                );

        List<Person> filtered =
                persons
                .stream()
                .filter(p->p.name.startsWith("P"))
                .collect(Collectors.toList());

        System.out.println("Names on P:" + filtered);

        Map<Integer, List<Person>> personsByAge = persons
                .stream()
                .collect(Collectors.groupingBy(p->p.age));
        personsByAge
                .forEach((age,p)->System.out.format("age %s: %s\n", age, p));

        Double averageAge = persons
                .stream()
                .collect(Collectors.averagingInt(p->p.age));
        System.out.println("Average age is: " + averageAge);
        IntSummaryStatistics ageSummary =
                persons
                .stream()
                .collect(Collectors.summarizingInt(p->p.age));
        System.out.println("Age summary is: " + ageSummary);
        System.out.println("Extract all who are of legal age:");
        persons
                .stream()
                .filter(p->p.age>18)
                .map(p->p.name)
                .forEach(System.out::println);

        String phrase = persons
                .stream()
                .filter(p->p.age>18)
                .map(p->p.name)
                .collect(Collectors.joining(" and ", "In Germany ", " are of legal age."));
        System.out.println(phrase);

        Map<Integer, String> map = persons
                .stream()
                .collect(Collectors.toMap(
                        p->p.age,
                        p->p.name,
                        (name1,name2)->name1 + ";" + name2
                ));
        System.out.println(map);

        Flatmap flatmap = new Flatmap();
        flatmap.flatMapExample();
    }



}
