[英]iteration over hashmap list
我想遍歷HashMap列表並檢索鍵和值(值1和value2)。 這行有一條錯誤,提示“類型不匹配:無法從元素類型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();
}
將Map.entrySet()
更改為PropertyMap.entrySet()
或conceptMap.entrySet()
Map接口聲明的Map.entrySet()
方法返回地圖的集合視圖(返回Set
)。 每個set元素都是一個Map.Entry
對象。 獲取對地圖條目的引用的唯一方法是從此collection-view的迭代器中進行。
如果要返回插入地圖的Set
,則必須在放置它的Collection上調用它:
PropertyMap.entrySet()
和conceptMap.entrySet()
將返回Set。
Map.entrySet()
不在任何實例化的Maps
上調用該方法。
Map.entrySet()返回地圖的集合視圖。將其更改為conceptMap.entrySet()或propertyMap.entrySet
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.