[英]iteration over hashmap list
I want to iterate over the HashMap list and retrieve the key and the values (value 1 and value2). 我想遍历HashMap列表并检索键和值(值1和value2)。 There is a error at this line that says "Type mismatch: cannot convert from element type Object to Map.Entry>"
这行有一条错误,提示“类型不匹配:无法从元素类型Object转换为Map.Entry>”
for (Map.Entry<String, List<String>> entry : Map.entrySet())
Am I doing anything wrong. 我做错什么了吗? Please help me out.
请帮帮我。 Here is the entire code.
这是完整的代码。
public static void main(String[] args) {
Map<String, List<String>> conceptMap = new HashMap<String, List<String>>();
Map<String, List<String>> PropertyMap = new HashMap<String, List<String>>();
try{
Scanner scanner = new Scanner(new FileReader("C:/"));
while (scanner.hasNextLine()){
String nextLine = scanner.nextLine();
String [] column = nextLine.split(":");
if (column[0].equals ("Property")){
if (column.length == 4) {
PropertyMap.put(column [1], Arrays.asList(column[2], column[3]));
}
else {
conceptMap.put (column [1], Arrays.asList (column[2], column[3]));
}
}
for (Map.Entry<String, List<String>> entry : Map.entrySet()) {
String key = entry.getKey();
List<String> valueList = entry.getValue();
System.out.println("Key: " + key);
System.out.print("Values: ");
for (String s : valueList) {
System.out.print(s + " ");
}
}
}
scanner.close();
}
catch (Exception e) {
e.printStackTrace();
}
将Map.entrySet()
更改为PropertyMap.entrySet()
或conceptMap.entrySet()
The Map.entrySet()
method declared by the Map interface returns a collection-view of the map (returns a Set
). Map接口声明的
Map.entrySet()
方法返回地图的集合视图(返回Set
)。 Each of these set elements is a Map.Entry
object. 每个set元素都是一个
Map.Entry
对象。 The only way to obtain a reference to a map entry is from the iterator of this collection-view. 获取对地图条目的引用的唯一方法是从此collection-view的迭代器中进行。
If you want to return a Set
you inserted into the map, you have to call it on the Collection you placed it in: 如果要返回插入地图的
Set
,则必须在放置它的Collection上调用它:
PropertyMap.entrySet()
and conceptMap.entrySet()
will return Sets. PropertyMap.entrySet()
和conceptMap.entrySet()
将返回Set。
Map.entrySet()
is not calling the method on either of your instantiated Maps
. Map.entrySet()
不在任何实例化的Maps
上调用该方法。
Map.entrySet()返回地图的集合视图。将其更改为conceptMap.entrySet()或propertyMap.entrySet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.