Lambda 笔记

对集合分组

        Person p1 = new Person(1L, "a", 1, "aaaa");
        Person p2 = new Person(2L, "a", 1, "bbbb");
        Person p3 = new Person(3L, "b", 1, "bbbb");
        Person p4 = new Person(4L, "b", 1, "aaaa");
        Person p5 = new Person(5L, "c", 1, "bbbb");
        List<Person> personList = Arrays.asList(p1, p2, p3, p4, p5);
        //List<Person> personList = Arrays.asList();
        Map<String, List<Person>> collect_adds = personList.stream().filter(p->p.getId()>3L).collect(Collectors.groupingBy(Person::getAdds));
        collect_adds.forEach((adds, persons) -> {
            System.out.println(MessageFormat.format("adds:{0},person:{1}", adds, persons));
        });

结果:

adds:aaaa,person:[Person{id=4, name='b', age=1, adds='aaaa'}]
adds:bbbb,person:[Person{id=5, name='c', age=1, adds='bbbb'}]

猜你喜欢

转载自www.cnblogs.com/lanqie/p/9431830.html