将键值作为另一个地图的新值与 java 中的重复项合并

combine key value as a new value for another map with duplicates in java

我将 map1 作为 <k1,v1>,我必须创建 map2 并将 map1 作为值,例如 map2=<k3,map1>

但是键 k1k3 有重复项,我们必须保留重复项。

示例

map1={(1,a),(1,b),(2,c)}
map2={5={1,a},5={1,b},6={2,c}}

如何使用 hashmapsmaps(不使用 guava 的多图概念)实现此目的

由于 HashMap 不允许存储重复键,您可能需要考虑稍微更改代码并创建 HashMap<Key,ArrayList<HashMap>>。具有相同密钥的 Map 将存储在 HashMapArrayList 中的相同 Key 下。 “HashMaps 的 ArrayList”将是 parent HashMapvalue。有一个简单的例子,我如何看到你可以实现类似于包含更多值来复制键的东西(我使用硬编码键值使其更容易阅读,我还在代码的注释中添加了一些解释):

import java.util.*;

public class A {
    public static void main(String[] args) {
        Map<Integer, ArrayList<Character>> map1 = new HashMap<>(); // Map containing few Characters under one key
        map1.put(1, new ArrayList<Character>()); 
        map1.get(1).add('a');
        map1.get(1).add('b');
        map1.put(2, new ArrayList<Character>());
        map1.get(2).add('c');
        System.out.println("map1: " + map1); // prints: map1: {1=[a, b], 2=[c]}
        Map<Integer, ArrayList<HashMap<Integer, Character>>> map2 = new HashMap<>(); // Map storing as keys 5 and 6, values are maps from map1
        map2.put(5, new ArrayList<HashMap<Integer, Character>>());
        map2.put(6, new ArrayList<HashMap<Integer, Character>>());
        for(Map.Entry<Integer, ArrayList<Character>> entry : map1.entrySet()) { // For each Integer-ArrayList pair from map1...
            if(entry.getKey().equals(1)) { // Check if key Integer is equal to 1, if it is...
                for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
                    HashMap<Integer, Character> innerMap = new HashMap<>();
                    innerMap.put(entry.getKey(), character);
                    map2.get(5).add((new HashMap<Integer,Character>(innerMap)));
                }
            }
            if(entry.getKey().equals(2)) { // Check if key Integer is equal to 1, if it is...
                for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
                    HashMap<Integer, Character> innerMap = new HashMap<>();
                    innerMap.put(entry.getKey(), character);
                    map2.get(6).add((new HashMap<Integer,Character>(innerMap)));
                }
            }

        }
        System.out.println("map2: " + map2); // prints: map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}
    }
}

你得到的输出:

map1: {1=[a, b], 2=[c]}
map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}

您可以使用结果映射以及 getKey()getValue() 方法的组合来创建 Map,如 5={1,a}5=[{1=b} 移动(但请注意,您不能将它们存储在同一个 Map 中)。

地图不允许对任何给定键进行多次映射。

不过,您可以为 map1 创建一个 Map<K1, List<V1>>,它将键映射到值列表。

要添加条目,而不是使用 Map.put,请使用 Map.computeIfAbsent:

map1.computeIfAbsent(1, k -> new ArrayList<>()).add("a");
map1.computeIfAbsent(1, k -> new ArrayList<>()).add("b");
map1.computeIfAbsent(2, k -> new ArrayList<>()).add("c");

它具有以下结构:

map1: {1=[a, b], 2=[c]}

现在,对于 map2,尚不清楚您是否需要采用相同的方法,或者是否使用 Map.put