小编Paw*_*Bąk的帖子

使用流将Map <A,Map <B,C >>转换为Map <B,Map <A,C >>

我有地图的结构图,如:

Map<Center, Map<Product, Value>> given
Run Code Online (Sandbox Code Playgroud)

而且我想得到

Map<Product, Map<Center, Value>> result
Run Code Online (Sandbox Code Playgroud)

我使用过Java流

Map<Product, Map<Center, Value>> result = given.entrySet().stream()
        .flatMap(entry -> entry.getValue()
                 .entrySet().stream()
                 .map(e -> Triple(entry.getKey(), e.getKey(), e.getValue())))
        .collect(Collectors.groupingBy(Triple::getProduct,
                    Collectors.toMap(Triple::getCenter, Triple::getValue)));
Run Code Online (Sandbox Code Playgroud)

哪里Triple是简单的价值类.我的问题是,如果有可能做到这一点的功能,而无需使用额外的类,如Triple或如Table从番石榴?

java java-8 java-stream

3
推荐指数
1
解决办法
97
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1