简体   繁体   English

Java 流收集以使用多个键进行映射

[英]Java stream collect to map with multiple keys

I thought I was getting pretty good at Java 8 streams, but then...我以为我已经很擅长 Java 8 流了,但后来……

I have a Foo interface:我有一个Foo接口:

public interface Foo {
  String getKey();
  Stream<Bar> bars();
}

I know I can collect a Stream<Foo> into a Map<String, Foo> using the key of each:我知道我可以使用每个键将Stream<Foo>收集到Map<String, Foo>中:

Map<String, Foo> foosByKey = fooStream.collect(
    Collectors.toMap(Foo::getKey, Function.identity()));

But what if I want to collect them into a Map<Bar, Foo> ?但是如果我想将它们收集到Map<Bar, Foo>怎么办? In other words, for each Foo in the steam, I want to put that Foo in the map keyed to every one of the Bar instances returned by Foo.bars() .换句话说,对于 Steam 中的每个Foo ,我想将该Foo放入映射到每个由Foo.bars()返回的Bar实例的Foo.bars() Where do I start?我从哪里开始?

As suggested here , you'll want extract the Bar values from each Foo and create pairs of them.正如此处所建议的,您需要从每个Foo提取Bar值并创建它们对。 Once you have the pairs, you can collect them into a Map .获得对后,您可以将它们收集到Map For example,例如,

Map<Bar, Foo> map = fooStream.flatMap(foo -> foo.bars().map(bar -> new SimpleEntry<>(bar, foo)))
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); 

We use SimpleEntry here because it's available (Java doesn't have a simpler Pair type).我们在这里使用SimpleEntry是因为它可用(Java 没有更简单的Pair类型)。 You could write your own to be more specific.你可以自己写一个更具体的。

You could define a new collector for that.您可以为此定义一个新的收集器。 One simple implementation (that always creates HashMap's of ArrayList's; no downstream support) could be:一个简单的实现(总是创建 ArrayList 的 HashMap;没有下游支持)可以是:

public static <T, K>
Collector<T, ?, Map<K, List<T>>> multiGroupingBy(
        Function<? super T, Collection<? extends K>> multiClassifier) {
    return Collector.of(
            HashMap::new,
            (map, entry) -> {
                multiClassifier.apply(entry)
                        .forEach(
                                key -> map
                                        .computeIfAbsent(key,
                                                __ -> new ArrayList<>())
                                        .add(entry));
            },
            (map1, map2) -> {
                map2.forEach(
                        (key, list) -> map1
                                .computeIfAbsent(key,
                                        __ -> new ArrayList<>())
                                .addAll(list));
                return map1;
            });
}

Then you could call:然后你可以调用:

fooStream.collect(multiGroupingBy(Foo::bars));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM