Java 8-如何在stream.groupingBy中使用当前项目

尼古拉斯

我有String列表作为输入,并且有函数接收String作为参数并返回对象列表。我想将其包装在map中,其中键-列表中的当前String,以及对象列表作为值-产生该功能。

List<String> keys = List.of("one", "two", "three");
public List<Entity> someFunction(String param) throws IOException;

//how to put it into map? 
private Mapt<String, List<Entity>> getEntities()  {
    Map<String, List<Entity>> map = keys.stream()
        .collect(Collectors.groupingBy(Function.identity(), 
             client.someFunction(?)));//how to pass current item to "someFunction"? and probably whap the exception
    return map;
}
他们是

Collectors.groupingBy似乎不适合使用合适的收集器,因为您似乎没有进行分组,只是将一个值映射到input的每个键List

用途toMap

private Map<String, List<Entity>> getEntities()  {
    return 
        keys.stream()
            .collect(Collectors.toMap(Function.identity(), 
                                      k -> client.someFunction(k)));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章