在嵌套数据结构上使用Java8流创建映射

goo*_*her 5 java map nested-loops java-8 java-stream

我正在使用Java 8的流搜索这段代码的优雅等价物:

Collection<X> xs = ...;
Map<B, A> map = new SomeMap<>();

for (X x : xs) {
    A a = x.getA();
    Collection<B> bs = x.getBs();

    for (B b : bs)
        map.put(b, a);
}
Run Code Online (Sandbox Code Playgroud)

这对我来说有点太棘手,因为我无法想到使用flatMap和Collectors.toMap组合实现所需的功能.

可编辑的例子:

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Application {

    public static class A {}

    public static class B {}

    public static class X {
        private A a;
        private Collection<B> bs;

        public X(A a, Collection<B> bs) {
            this.a = a;
            this.bs = bs;
        }

        public A getA() {
            return a;
        }

        public Collection<B> getBs() {
            return bs;
        }
    }

    public static void main(String[] args) {
        X x1 = new X(new A(), Arrays.asList(new B(), new B()));
        X x2 = new X(new A(), Arrays.asList(new B()));

        Collection<X> xs = Arrays.asList(x1, x2);

        Map<B, A> map = new HashMap<>();

        for (X x : xs) {
            A a = x.getA();
            Collection<B> bs = x.getBs();

            for (B b : bs)
                map.put(b, a);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

ass*_*ias 5

正如您所想,可以使用flatMap和toMap的混合来实现:

Map<B, A> map = xs.stream()
                  .flatMap(x -> x.getBs().stream()
                                 .map(b -> new SimpleEntry<> (b, x.getA())))
                  .collect(toMap(Entry::getKey, Entry::getValue));
Run Code Online (Sandbox Code Playgroud)

请注意,如果存在重复的B,此代码与原始代码不同:您的代码将保留覆盖相应的值,而此代码将引发异常.