[英]Creating a Map using Java8 streams on a nested Data Structure
I am searching for an elegant equivalent of this piece of code using Java 8's streams: 我正在使用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);
}
That one is a bit too tricky for me as I can't think of a combination using flatMap and Collectors.toMap which would implement the desired functionality. 这对我来说有点棘手,因为我想不出使用flatMap和Collectors.toMap的组合来实现所需的功能。
Compilable example: 编译示例:
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);
}
}
}
As you thought you can do it with a mixture of flatMap and toMap: 如您所愿,可以将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));
Note that this code differs from your original if there are duplicate Bs: your code will keep overwriting the corresponding value whereas this code will throw an exception. 请注意,如果B重复,则此代码与原始代码不同:您的代码将继续覆盖相应的值,而此代码将引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.