【发布时间】: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