【问题标题】:Want to concatenate Value of one map to key of another map想要将一个映射的值连接到另一个映射的键
【发布时间】:2019-10-07 02:32:55
【问题描述】:

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

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}

【问题讨论】:

  • “我曾尝试使用 iterator 和 entryset 来遍历这两个地图,但没有成功” 告诉我们你尝试了什么,否则我们如何帮助你弄清楚你做了什么错误的?现在,问题似乎是试图让我们为您编写代码。向您展示尝试,我们会提供帮助。
  • 提示:迭代map2,在map1中查找
  • 嘿@Andreas,我更新了代码,但没有用。 IK 不能用 map2 迭代它,因为 map2 包含同一个键​​的多个值,我在 map1 中有唯一键

标签: java collections hashmap hashtable


【解决方案1】:

我用

实现了这一点

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);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多