我想遍历 HashMap 列表并检索键和值(值 1 和值 2)。此行有一个错误,显示“类型不匹配:无法从元素类型 Object 转换为 Map.Entry>”
for (Map.Entry<String, List<String>> entry : Map.entrySet())
我做错什么了吗。请帮帮我。这是整个代码。
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();
}