java 从java中的hashMaps列表的值中获取键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35752316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the key from the value of a list of hashMaps in java
提问by Eric G
I have 4 hashMaps:
我有 4 个 hashMap:
Map <String, Integer> item1 =new HashMap<String, Integer>();
Map <String, Integer> item2 =new HashMap<String, Integer>();
Map <String, Integer> item3 =new HashMap<String, Integer>();
Map <String, Integer> item4 =new HashMap<String, Integer>();
I added the 4 of them to a list:
我将其中的 4 个添加到列表中:
List<Map> Items= new ArrayList<>();
Items.add(item1);
Items.add(item2);
Items.add(item3);
Items.add(item4);
I wanted to get the key from the value using the list of hashMaps "Items" So I tried this code:
我想使用 hashMaps "Items" 列表从值中获取键所以我尝试了这个代码:
for(int i=0; i<n; i++)
{
String key= null;
int value=v;
for(Map.Entry entry: Items.get(i).entrySet()){
if(value == (entry.getValue())){
key = (String) entry.getKey();
break;
}
}
But the Items.get(i).entrySet() doesn't work!
但是 Items.get(i).entrySet() 不起作用!
I get these errors:
我收到这些错误:
-Map.Entry is a raw type. References to generic type Map<K,V>.Entry<K,V> should be parameterized
-Type mismatch: cannot convert from element type Object to Map.Entry
-Incompatible operand types int and Object
Does anyone know how to do it?
有谁知道怎么做?
回答by Eric G
First remake the List variable to include the types of the map. This will make the .entrySet()
return the correct formatting of the entry Map<String,Integer>
首先重新创建 List 变量以包含地图的类型。这将使.entrySet()
返回的条目格式正确Map<String,Integer>
List<Map<String, Integer>> items = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
String key = null;
int value = 0;
for (Map.Entry<String, Integer> entry: items.get(i).entrySet()) {
if (value == (entry.getValue())) {
key = (String) entry.getKey();
break;
}
}
}
回答by Bahramdun Adil
You can try this
你可以试试这个
First add the generic part <String, Integer>
to the Map
of List
as below:
首先将通用部分添加<String, Integer>
到Map
of 中List
,如下所示:
List<Map<String, Integer>> Items= new ArrayList<>();
And then:
接着:
for (Map<String, Integer> item : Items) {
for (Map.Entry<String, Integer> entry: items.get(i).entrySet()) {
// do what you want with the entry
}
}
回答by SkyWalker
You can use it. It will work fine.
你可以使用它。它会正常工作。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
Map <String, Integer> item1 =new HashMap<String, Integer>();
Map <String, Integer> item2 =new HashMap<String, Integer>();
Map <String, Integer> item3 =new HashMap<String, Integer>();
Map <String, Integer> item4 =new HashMap<String, Integer>();
List<Map<String, Integer>> Items= new ArrayList<Map<String, Integer>>();
Items.add(item1);
Items.add(item2);
Items.add(item3);
Items.add(item4);
for (int i = 0; i < Items.size(); i++) {
String key = null;
int value = 0;
for (Map.Entry<String, Integer> entry: Items.get(i).entrySet()) {
if (value == (entry.getValue())) {
key = (String) entry.getKey();
break;
}
}
}
}
}