我的意见是:

List<Map<String, String>> = [{1=a, 2=b, 3=c},
                             {1=d, 2=e, 3=f}]
List<String> = [x, y, z]

预期输出为:

[
{1=a, 2=b, 3=c, 4=x},
{1=a, 2=b, 3=c, 4=y},
{1=a, 2=b, 3=c, 4=z},
{1=d, 2=e, 3=f, 4=x},
{1=d, 2=e, 3=f, 4=y},
{1=d, 2=e, 3=f, 4=z}
]

我的代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ExplodeData {
    public static void main(String[] args) {
        List<Map<String, String>> listOfMap = List.of(
        new HashMap<>(Map.of("1", "a", "2", "b", "3", "c")),
        new HashMap<>(Map.of("1", "d", "2", "e", "3", "f"))
        );
        List<String> stringList = new ArrayList<>();
        stringList.add("x");
        stringList.add("y");
        stringList.add("z");

        listOfMap.forEach(System.out::println);
        System.out.println(stringList + "\n\n");

        List<Map<String, String>> result = new ArrayList<>();

        for (Map<String, String> eachMap: listOfMap) {
            for (String eachString: stringList) {
                Map<String, String> newEntry = new HashMap<>();
                newEntry.putAll(eachMap);
                newEntry.put("4", eachString);
                result.add(newEntry);
            }
        }
        System.out.println("Expected Result using for loops");
        result.forEach(System.out::println);

        List<Map<String, String>> result2 = stringList
                .stream()
                .flatMap(each -> listOfMap.stream().peek(entry -> entry.put("4", each)))
                .collect(Collectors.toList());

        System.out.println("\n\nResult using streams");
        result2.forEach(System.out::println);
    }
}

问题:我想将给出正确结果的for循环转换为stream.我当前的流代码给出了正确大小的结果,即(listOfMap*stringList),但由于浅拷贝,新键的值被覆盖.

电流输出

Expected Result using for loops
{1=a, 2=b, 3=c, 4=x}
{1=a, 2=b, 3=c, 4=y}
{1=a, 2=b, 3=c, 4=z}
{1=d, 2=e, 3=f, 4=x}
{1=d, 2=e, 3=f, 4=y}
{1=d, 2=e, 3=f, 4=z}


Result using streams
{1=a, 2=b, 3=c, 4=z}
{1=d, 2=e, 3=f, 4=z}
{1=a, 2=b, 3=c, 4=z}
{1=d, 2=e, 3=f, 4=z}
{1=a, 2=b, 3=c, 4=z}
{1=d, 2=e, 3=f, 4=z}

谢谢你的帮助.

推荐答案

你可以使用flatMap()和一个生成new个映射流的函数,就像循环版本一样,然后将所有内容收集回一个列表.流版本会修改现有的 map ,并不断用新的元素覆盖以前添加的"4"个元素.

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Demo {
    public static void main(String[] args) {
        List<Map<String, String>> listOfMap =
            List.of(Map.of("1", "a", "2", "b", "3", "c"),
                    Map.of("1", "d", "2", "e", "3", "f"));
        List<String> stringList = List.of("x", "y", "z");

        List<Map<String, String>> result =
            listOfMap.stream()
            .flatMap(map -> stringList.stream().map(elem -> {
                        Map<String, String> newMap = new HashMap<>(map);
                        newMap.put("4", elem);
                        return newMap;
                    }))
            .collect(Collectors.toList());

        for (Map<String, String> elem : result) {
            System.out.println(elem);
        }
    }
}

输出

{1=a, 2=b, 3=c, 4=x}
{1=a, 2=b, 3=c, 4=y}
{1=a, 2=b, 3=c, 4=z}
{1=d, 2=e, 3=f, 4=x}
{1=d, 2=e, 3=f, 4=y}
{1=d, 2=e, 3=f, 4=z}

Java相关问答推荐

在for—each循环中的AnimationTimer中的if语句'

使用JdkClientHttpRequestFactory通过Spring RestClient和Wiemock读取时达到EOF

Java函数式编程中的双值单值映射

给定Java枚举类,通过值查找枚举

确定Java中Math.Ranb()输出的上限

编译多个.Java文件并运行一个依赖于用户参数的文件

具有多种令牌类型和段的复杂Java 17正则表达式

由于 list 中的权限错误,Android未生成

Com.google.firebase.database.DatabaseException:无法将类型为java.lang.Boolean的值转换为字符串.这是关于什么的?

生成桥方法以解决具有相同擦除的冲突方法

带错误BER验证的itext8签名返回pdf

try 从REST API返回对象列表时出错

如何在JavaFX循环中完美地制作一个AudioClip/MediaPlayer?

如何以编程方式保存workBench.xmi?

JavaFX标签中的奇怪字符

有没有办法在o(log(N))中以系统的方式将数组中的小块元素复制和移动到新增长的数组中的左侧?

在实例化中指定泛型类型与不指定泛型类型之间的区别

为什么创建Java动态代理需要接口参数

如何在Java中立即显示ProgressBar对话框?

如何使 JavaFx 中的表视图响应式?