Java 如何反转HashMap的键和值?
我已经创建了一个Java 如何反转HashMap的键和值?,java,dictionary,hashmap,iterator,Java,Dictionary,Hashmap,Iterator,我已经创建了一个HashMap。现在,我想通过替换第一个映射中的键和值来创建一个反向HashMap 比如说, 原始HashMap:{A=[2,1],B=[1,3,4],C=[5],D=[3],E=[2,4]} 反向哈希映射:{1=[A,B],2=[A,E],3=[B,D],4=[B,E],5=[C]}实现以下伪代码: 参数:原始映射OM 让RM是一个空映射。 对于OM中的所有字符串: 对于OM[S]中的所有整数I: 如果RM不包含I: 设RM[I]为空列表。 将S添加到RM[I]。 返回RM。
HashMap
。现在,我想通过替换第一个映射中的键和值来创建一个反向HashMap
比如说,
原始HashMap:{A=[2,1],B=[1,3,4],C=[5],D=[3],E=[2,4]}
反向哈希映射:
{1=[A,B],2=[A,E],3=[B,D],4=[B,E],5=[C]}
实现以下伪代码:
参数:原始映射OM
让RM是一个空映射。
对于OM中的所有字符串:
对于OM[S]中的所有整数I:
如果RM不包含I:
设RM[I]为空列表。
将S添加到RM[I]。
返回RM。
执行以下伪代码:
参数:原始映射OM
让RM是一个空映射。
对于OM中的所有字符串:
对于OM[S]中的所有整数I:
如果RM不包含I:
设RM[I]为空列表。
将S添加到RM[I]。
返回RM。
试试这个
HashMap<Integer,List<String>> reverse = new HashMap<>(original.size());
for(HashMap.Entry<String,List<Integer>> entry : original) {
for(Integer n : entry.getValue()) {
if(!reverse.containsKey(n)) { //Reverse doesn't have this number yet, create a new list at that key.
reverse.add(n,new ArrayList<>());
}
reverse.get(n).add(entry.getKey());
}
}
HashMap reverse=新的HashMap(original.size());
for(HashMap.Entry:原始){
for(整数n:entry.getValue()){
如果(!reverse.containsKey(n)){//reverse还没有此编号,请在该键处创建一个新列表。
add(n,newarraylist());
}
reverse.get(n.add(entry.getKey());
}
}
让我知道,如果有一些错误,但修补它首先 试试这个
HashMap<Integer,List<String>> reverse = new HashMap<>(original.size());
for(HashMap.Entry<String,List<Integer>> entry : original) {
for(Integer n : entry.getValue()) {
if(!reverse.containsKey(n)) { //Reverse doesn't have this number yet, create a new list at that key.
reverse.add(n,new ArrayList<>());
}
reverse.get(n).add(entry.getKey());
}
}
HashMap<String, List<Integer>> hMap=new HashMap<String, List<Integer>>();
hMap.put("A",new ArrayList<Integer>(Arrays.asList(2,1)));
hMap.put("B",new ArrayList<Integer>(Arrays.asList(1,3,4)));
hMap.put("C",new ArrayList<Integer>(Arrays.asList(5)));
hMap.put("D",new ArrayList<Integer>(Arrays.asList(3)));
hMap.put("E",new ArrayList<Integer>(Arrays.asList(2,4)));
//Original HashMap: { A=[2,1], B=[1,3,4], C=[5], D=[3], E=[2,4] }
//Reversed HashMap: { 1=[A,B], 2=[A,E], 3=[B,D], 4=[B,E], 5=[C] }
HashMap<Integer,List<String>> result = new HashMap<>(hMap.size());
for(Map.Entry<String,List<Integer>> entry : hMap.entrySet()) {
for(Integer n : entry.getValue()) {
if(!result.containsKey(n)) {
result.put(n,new ArrayList<String>());
}
result.get(n).add(entry.getKey());
}
}
System.out.println(hMap);
System.out.println(result);
HashMap reverse=新的HashMap(original.size());
for(HashMap.Entry:原始){
for(整数n:entry.getValue()){
如果(!reverse.containsKey(n)){//reverse还没有此编号,请在该键处创建一个新列表。
add(n,newarraylist());
}
reverse.get(n.add(entry.getKey());
}
}
让我知道,如果有一些错误,但修补它首先 HashMap hMap=newhashmap();
HashMap<String, List<Integer>> hMap=new HashMap<String, List<Integer>>();
hMap.put("A",new ArrayList<Integer>(Arrays.asList(2,1)));
hMap.put("B",new ArrayList<Integer>(Arrays.asList(1,3,4)));
hMap.put("C",new ArrayList<Integer>(Arrays.asList(5)));
hMap.put("D",new ArrayList<Integer>(Arrays.asList(3)));
hMap.put("E",new ArrayList<Integer>(Arrays.asList(2,4)));
//Original HashMap: { A=[2,1], B=[1,3,4], C=[5], D=[3], E=[2,4] }
//Reversed HashMap: { 1=[A,B], 2=[A,E], 3=[B,D], 4=[B,E], 5=[C] }
HashMap<Integer,List<String>> result = new HashMap<>(hMap.size());
for(Map.Entry<String,List<Integer>> entry : hMap.entrySet()) {
for(Integer n : entry.getValue()) {
if(!result.containsKey(n)) {
result.put(n,new ArrayList<String>());
}
result.get(n).add(entry.getKey());
}
}
System.out.println(hMap);
System.out.println(result);
hMap.put(“A”,新的ArrayList(Arrays.asList(2,1));
hMap.put(“B”,新的ArrayList(Arrays.asList(1,3,4));
hMap.put(“C”,新的ArrayList(Arrays.asList(5));
hMap.put(“D”,新的数组列表(Arrays.asList(3));
hMap.put(“E”,新的ArrayList(Arrays.asList(2,4));
//原始哈希映射:{A=[2,1],B=[1,3,4],C=[5],D=[3],E=[2,4]}
//反向哈希映射:{1=[A,B],2=[A,E],3=[B,D],4=[B,E],5=[C]}
HashMap结果=新的HashMap(hMap.size());
对于(Map.Entry:hMap.entrySet()){
对于(整数n:entry.getValue()){
如果(!result.containsKey(n)){
result.put(n,newarraylist());
}
result.get(n.add(entry.getKey());
}
}
系统输出打印LN(hMap);
系统输出打印项次(结果);
输出
{D=[3],E=[2,4],A=[2,1],B=[1,3,4],C=[5]}
{1=[A,B],2=[E,A],3=[D,B],4=[E,B],5=[C]}HashMap hMap=newhashmap();
hMap.put(“A”,新的ArrayList(Arrays.asList(2,1));
hMap.put(“B”,新的ArrayList(Arrays.asList(1,3,4));
hMap.put(“C”,新的ArrayList(Arrays.asList(5));
hMap.put(“D”,新的数组列表(Arrays.asList(3));
hMap.put(“E”,新的ArrayList(Arrays.asList(2,4));
//原始哈希映射:{A=[2,1],B=[1,3,4],C=[5],D=[3],E=[2,4]}
//反向哈希映射:{1=[A,B],2=[A,E],3=[B,D],4=[B,E],5=[C]}
HashMap结果=新的HashMap(hMap.size());
对于(Map.Entry:hMap.entrySet()){
对于(整数n:entry.getValue()){
如果(!result.containsKey(n)){
result.put(n,newarraylist());
}
result.get(n.add(entry.getKey());
}
}
系统输出打印LN(hMap);
系统输出打印项次(结果);
输出
{D=[3],E=[2,4],A=[2,1],B=[1,3,4],C=[5]}
{1=[A,B],2=[E,A],3=[D,B],4=[E,B],5=[C]}你的代码有什么问题?你不是要求我们为你写,对吧:)我相信你的意思是反转,还原意味着“返回到以前的状态”创建一个新的
映射和循环。两次。完成。您的代码有什么问题?你不是要求我们为你写,对吧:)我相信你的意思是反转,还原意味着“返回到以前的状态”创建一个新的映射和循环。两次。完成。