使用PartioningBy和groupingBy时无法获得所需的输出

时间:2016-12-05 11:26:00

标签: java java-8 java-stream partitioning collectors

我正在收集者的groupingBy和partioningBy函数。我正在处理一份人员名单,人员名单如下:

List<Person> persons =
        Arrays.asList(
            new Person("Max", 18),
            new Person("Peter", 23),
            new Person("Pamela", 23),
            new Person("David", 12),
            new Person("Pam", 12));

我想要的是根据名字以字母“P”开头的人对列表进行分区,然后根据他们的年龄对其进行分组。 这是我的代码,它执行上述过滤:

Map<Boolean, Map<Object, List<Person>>> rr = persons.stream()
                                            .collect(Collectors.partitioningBy(p -> p.name.startsWith("P"), 
                                                    Collectors.groupingBy(p -> p.age > 20)));

我得到的输出是:

rr = {false={false=[Max, David]}, true={false=[Pam], true=[Peter, Pamela]}}

现在,我的要求是只从上面的结果中获取内部地图。也就是说,我想将返回值更改为:

{false=[Pam], true=[Peter, Pamela]}

也就是说,我想要partioningBy函数返回的布尔值为true的结果(或分区映射)。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

  1. name
  2. 过滤
  3. age

    应用分区
        Map<Boolean, List<Person>> p1 = persons.stream().filter(p -> p.name.startsWith("P")).collect(Collectors.partitioningBy(p -> p.getAge() > 20));
    

答案 1 :(得分:1)

你可以创建一个自定义收藏家(我只是作为一个练习完成它,请这样对待):

 static class MyCustom<T, U> implements Collector<Person, List<Person>, Map<T, List<U>>> {

    private final Function<Person, T> function;

    private final Predicate<Person> predicate;

    private final Function<Person, U> transformingFunction;

    public MyCustom(Predicate<Person> predicate, Function<Person, T> function,
            Function<Person, U> transformingFunction) {
        this.predicate = predicate;
        this.function = function;
        this.transformingFunction = transformingFunction;
    }

    @Override
    public Supplier<List<Person>> supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<Person>, Person> accumulator() {
        return (list, person) -> {
            if (predicate.test(person)) {
                list.add(person);
            }
        };
    }

    @Override
    public BinaryOperator<List<Person>> combiner() {
        return (l1, l2) -> {
            l1.addAll(l2);
            return l1;
        };
    }

    @Override
    public Function<List<Person>, Map<T, List<U>>> finisher() {
        return list -> {
            return list.stream().collect(
                    Collectors.groupingBy(function, Collectors.mapping(transformingFunction, Collectors.toList())));
        };
    }

    @Override
    public Set<java.util.stream.Collector.Characteristics> characteristics() {
        return EnumSet.of(Characteristics.UNORDERED);
    }
}

然后像这样应用:

 MyCustom<Integer, String> custom = new MyCustom<>((Person p) -> p.getName().startsWith("P"),
            (Person p) -> p.getAge(), Person::getName);

 System.out.println(persons.stream().collect(custom)); // {23=[Peter, Pamela], 12=[Pam]}