Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Collectors.toMap如何转换贴图值_Java_Java Stream_Collectors - Fatal编程技术网

Java 使用Collectors.toMap如何转换贴图值

Java 使用Collectors.toMap如何转换贴图值,java,java-stream,collectors,Java,Java Stream,Collectors,我有一张地图 我想将列表中的对象转换为另一个对象。 即Map 我可以使用java stream Collectors.toMap()实现这一点吗 我尝试了下面的代码 Map<String, List<StartingMaterial>> startingMaterialMap = xxxx; startingMaterialMap.entrySet().stream().collect(Collectors.toMap( Map.Entry::getKey, Func

我有一张
地图
我想将列表中的对象转换为另一个对象。 即
Map

我可以使用java stream Collectors.toMap()实现这一点吗

我尝试了下面的代码

Map<String, List<StartingMaterial>>  startingMaterialMap = xxxx;

startingMaterialMap.entrySet().stream().collect(Collectors.toMap( Map.Entry::getKey, Function.identity(), (k, v) -> convertStartingMaterialToDto(v.getValue())));

您可以使用toMap收集器,因为您的源是一个映射。但是,您必须迭代所有值,并在valueMapper中将它们转换为DTO格式

Map<String, List<StartingMaterialResponse>> result = startingMaterialMap.entrySet().stream()
    .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().stream()
        .map(s -> convertStartingMaterialToDto(s)).collect(Collectors.toList())));
Map result=startingMaterialMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,e->e.getValue().stream())
.map->convertStartingMaterialToDto.collect(collector.toList());

我想你的意思是:

startingMaterialMap.entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey,
                e -> e.getValue().stream()
                        .map(this::convertStartingMaterialToDto)
                        .collect(Collectors.toList()))
        );

以下是我解决这个问题的方法:

    Map<String, List<Integer>> deposits = new HashMap<>();

    deposits.put("first", Arrays.asList(1, 2, 3));

    deposits.forEach((depositName, products) -> {
        products.stream()
                .map(myIntegerProduct -> myIntegerProduct.toString())
                .collect(Collectors.toList());
    });
Map depositions=newhashmap();
存款。看跌期权(“第一”,数组。asList(1,2,3));
存款。forEach((存款名称、产品)->{
products.stream()
.map(myIntegerProduct->myIntegerProduct.toString())
.collect(Collectors.toList());
});
上面的示例将
列表
转换为字符串列表。 在您的示例中,convertStartingMaterialToDto方法不是
myIntegerProduct.toString()

forEach
方法迭代映射中的每个键值对,您可以为键值和值参数设置一些更具体的名称,并为每个阅读它的人保留一个可理解的代码。在我的示例中:
forEach((存款名称,产品))->
存款名称
是键(在我的示例中是字符串),产品是键的值(在我的示例中是整数列表)

最后,您也将遍历列表,并将每个项映射到一个新类型

products.stream()
.map(myIntegerProduct->myIntegerProduct.toString())

如果碰巧
map
是由
groupingBy
组成的,您可以预先提供。当然,这时您不想单独使用
Map
    Map<String, List<Integer>> deposits = new HashMap<>();

    deposits.put("first", Arrays.asList(1, 2, 3));

    deposits.forEach((depositName, products) -> {
        products.stream()
                .map(myIntegerProduct -> myIntegerProduct.toString())
                .collect(Collectors.toList());
    });