java8 stream flatmap

	Student s1 = new Student();
        s1.setName("jack");
        s1.addBook("java");
        s1.addBook("c++");
        s1.addBook("php");

        Student s2 = new Student();
        s2.setName("kobe");
        s2.addBook("python");
        s2.addBook(".net");

        List list = new ArrayList<>();
        list.add(s1);
        list.add(s2);

        List collect =
                list.stream()
                        .map(x -> x.getBook())      
                        .flatMap(x -> x.stream())  
                        .distinct()
                        .collect(Collectors.toList());

        collect.forEach(x -> System.out.println(x));
输出:java c++php python .net

你可能感兴趣的:(java)