gpt4 book ai didi

java - 使用java流,将两个具有相同键但不同值的映射合并到一个元组中?

转载 作者:行者123 更新时间:2023-11-30 06:03:31 25 4
gpt4 key购买 nike

我有两个具有以下数据类型的 map ,

Map<Pair<Long,String>, List<String>>  stringValues;
Map<Pair<Long,String>, List<Boolean>> booleanValues ;

我想将上面的映射合并到下面的数据结构中

Map<Pair<Long,String>, Pair<List<String>,List<Boolean>>>  stringBoolValues;

我的输入有两个键相同但值不同的映射。我想将它们组合成一对。我可以使用 java 流来实现吗?

最佳答案

其他简单的方法是这样的:

stringValues.forEach((key, value) -> {
Pair<List<String>, List<Boolean>> pair = new Pair<>(value, booleanValues.get(key));
stringBoolValues.put(key, pair);
});

stringBoolValues = stringValues
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey,
entry -> new Pair<>(entry.getValue(), booleanValues.get(entry.getKey()))));

像这样尝试:

Set<Pair<Long,String>> keys = new HashSet<>(stringValues.keySet());
keys.addAll(booleanValues.keySet());

keys.stream().collect(Collectors.toMap(key -> key,
key -> new Pair<>(stringValues.get(key), booleanValues.get(key))));

关于java - 使用java流,将两个具有相同键但不同值的映射合并到一个元组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51978383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com