Java 合并两个地图以获得地图<;字符串,Set<;信息>&燃气轮机;

Java 合并两个地图以获得地图<;字符串,Set<;信息>&燃气轮机;,java,collections,Java,Collections,如何合并两个贴图以获得一个新贴图,该贴图将一个键映射到一个集合。 例如: map1; map2地图; 现在我想得到的结果如下 Map<String, Set<Information>> resultMap; Map结果图; 试试这个 private Map<String, Set<Information>> mergeMap(Map<String, Information> map1, Map<String, Informat

如何合并两个贴图以获得一个新贴图,该贴图将一个键映射到一个集合。 例如:

map1;
map2地图;
现在我想得到的结果如下

Map<String, Set<Information>> resultMap;
Map结果图;
试试这个

private Map<String, Set<Information>> mergeMap(Map<String, Information> map1, Map<String, Information> map2) {
    Map<String, Set<Information>> resultMap = new HashMap<>();
    for (String key : map1.keySet()) {
        Set<Information> set;
        if(resultMap.containsKey(key))
            set = resultMap.get(key);
        else 
            set = new HashSet<>();
        set.add(map1.get(key));
        resultMap.put(key, set);
    }

    for (String key : map2.keySet()) {
        Set<Information> set;
        if(resultMap.containsKey(key))
            set = resultMap.get(key);
        else
            set = new HashSet<>();
        set.add(map2.get(key));
        resultMap.put(key, set);
    }

    return resultMap;
}
私有地图合并地图(地图1、地图2){
Map resultMap=new HashMap();
for(字符串键:map1.keySet()){
集合;
if(结果映射containsKey(键))
set=resultMap.get(键);
其他的
set=新的HashSet();
set.add(map1.get(key));
结果映射put(键,集合);
}
for(字符串键:map2.keySet()){
集合;
if(结果映射containsKey(键))
set=resultMap.get(键);
其他的
set=新的HashSet();
set.add(map2.get(key));
结果映射put(键,集合);
}
返回结果图;
}
这样做可以:

        Map<String, Information> map1;
        Map<String, Information> map2;

        Map<String, Set<Information>> resultMap = new HashMap<>();

        Set<String> keySet = new HashSet<>(map1.keySet());
        keySet.addAll(map2.keySet());

        keySet.stream()
                .forEach(t -> {
                    Set<Information> data = new HashSet<>();
                    if (map1.get(t) != null)  {
                        data.add(map1.get(t));
                    }
                    if (map2.get(t) != null)  {
                        data.add(map2.get(t));
                    }

                    resultMap.put(t, data);
                });
map1;
map2地图;
Map resultMap=new HashMap();
Set keySet=newhashset(map1.keySet());
addAll(map2.keySet());
keySet.stream()
.forEach(t->{
Set data=new HashSet();
if(map1.get(t)!=null){
data.add(map1.get(t));
}
if(map2.get(t)!=null){
data.add(map2.get(t));
}
结果映射put(t,数据);
});

请记住,只有当
信息
类重写了
equals方法

时,这才会按照您想要的方式工作。例如,您可以迭代两个原始映射的键集(
map1.keySet()
然后
map2.keySet()
),以填充
映射结果映射=新哈希集
)的新实例

每次添加值时,请将其添加到现有集,或在尚未添加值时添加到全新集

Optional<Set<Information>> data = Optional.of(resultMap.get(aKey1));
if (data.isPresent()) {
    data.get().add(map1.get(aKey1))
} else {
    final Set<Information> newSet = new HashSet<>();
    newSet.add(map1.get(aKey1));
    resultMap.put(aKey1, newSet);
}
Optional data=Optional.of(resultMap.get(aKey1));
if(data.isPresent()){
data.get().add(map1.get(aKey1))
}否则{
final Set newSet=new HashSet();
add(map1.get(aKey1));
结果映射put(aKey1,newSet);
}
您还可以尝试使用流的更实用的方法。

使用流:

Map<String, Information> map1;
Map<String, Information> map2;

Map<String, Set<Information>> result = Stream.of(map1, map2)
    .flatMap(m -> m.entrySet().stream())
    .collect(Collectors.groupingBy(Entry::getKey,
        Collectors.mapping(Entry::getValue, Collectors.toSet())));
map1;
map2地图;
映射结果=Stream.of(map1,map2)
.flatMap(m->m.entrySet().stream())
.collect(收集器).groupingBy(条目::getKey,
Collectors.mapping(Entry::getValue,Collectors.toSet());

@Jom已经用stream给出了一个非常好的答案
这只是使用流和地图功能的另一个答案

Map<String, Set<String>> resultMap = new HashMap<>();

Stream.of(map1, map2).flatMap(map -> map.entrySet().stream()).forEach(entry -> {
    resultMap.merge(entry.getKey(), Collections.singleton(entry.getValue()),
            (v1, v2) -> Stream.concat(v1.stream(), v2.stream()).collect(Collectors.toSet()));
});
Map resultMap=newhashmap();
Stream.of(map1,map2).flatMap(map->map.entrySet().Stream()).forEach(条目->{
resultMap.merge(entry.getKey()、Collections.singleton(entry.getValue()),
(v1,v2)->Stream.concat(v1.Stream(),v2.Stream()).collect(Collectors.toSet());
});

你能提供一个例子,说明你在输入和输出中有什么吗?请再次检查我的答案。这将创建映射,而不是映射
Map<String, Set<String>> resultMap = new HashMap<>();

Stream.of(map1, map2).flatMap(map -> map.entrySet().stream()).forEach(entry -> {
    resultMap.merge(entry.getKey(), Collections.singleton(entry.getValue()),
            (v1, v2) -> Stream.concat(v1.stream(), v2.stream()).collect(Collectors.toSet()));
});