想要将一个地图的值连接到另一个地图的键

Want to concatenate Value of one map to key of another map

我想将一个映射的值连接到另一个映射的键并将它们添加到列表中。 将基于第一个映射的键的值与另一个映射的值进行比较。 例如:

map1= {37=core__error_code_based, 153=core__app_dialog, 123=core__date}


map2={copy_2=37,button_back=37,button_cancel=153,button_confirm=153}

我的方法是在第一个循环中获取 map1 的键,然后在第二个循环中基于 map1 键迭代 map2 值。 这样我就得到了 map1 的值和 map2 的键,然后在字符串中连接。

List<String> finalKey=new ArrayList<>();
        Iterator<Map.Entry<String,String>> entrySet=map1.entrySet().iterator();
        Iterator<Map.Entry<String,String>> pageKey=map2.entrySet().iterator();
        while(entrySet.hasNext()){
            Map.Entry<String,String> entry = entrySet.next();
            Map.Entry<String,String> pageValue = pageKey.next();

            while(entry.getKey()==pageValue.getValue()){
                finalKey.add(entry.getValue()+"__"+pageValue.getKey());
            }
        }

我曾尝试使用迭代器和入口集来遍历这两个映射但没有成功

{core__error_code_based__copy_2,core__error_code_based__button_back,core__app_dialog__button_confirm,core__app_dialog__button_cancel}

我用

实现了这个

public class translatekeyName {
    static List<String> finalString = new ArrayList<>();

    public static Map<String, String> initialMap() {
        Map<String, String> map1 = new HashMap<>();
        map1.put("37", "core__error_code_based");
        map1.put("153", "core__app_dialog");
        return map1;
    }

    public static Map<String, String> secondMap() {
        Map<String, String> map2 = new HashMap<>();
        map2.put("copy_2", "37");
        map2.put("button_back", "37");
        map2.put("button_cancel", "153");
        map2.put("button_confirm", "153");
        return map2;
    }

    public List<String> concatenateString(Map page, Map source) {
        Map<String, String> moduleKey = page;
        Map<String, String> pageKey = source;
        List<String> temp;

        Iterator<Map.Entry<String, String>> entrySet = page.entrySet().iterator();
        Iterator<Map.Entry<String, String>> pageKeyset = source.entrySet().iterator();
        for (String value : moduleKey.keySet()) {
            temp = getallKeys(source, value);
            String tempValue = moduleKey.get(value);
            for (int i = 0; i < temp.size(); i++) {
                tempValue += "__" + temp.get(i);
                finalString.add(tempValue);
            }

        }

        return finalString;
    }

    static <K, V> List<K> getallKeys(Map<K, V> mapOfWords, V value) {
        List<K> keylist = null;


        if (mapOfWords.containsValue(value)) {

            keylist = new ArrayList<>();

            for (Map.Entry<K, V> entry : mapOfWords.entrySet()) {

                if (entry.getValue().equals(value)) {
                    keylist.add(entry.getKey());
                }
            }
        }

        return keylist;
    }

    public static void main(String[] args) {
        translatekeyName obj = new translatekeyName();
        obj.concatenateString(initialMap(), secondMap());
        System.out.println(finalString);
    }

}