簡體   English   中英

HashMap上的數組迭代

[英]Iteration of arrays over a HashMap

有沒有一種簡單的方法來迭代一個哈希圖中的所有數組?

例如:

 HashMap<String, ArrayList<String>>

我從HashValue之一的數組中搜索元素。

Map<String, List<String>> map = new HashMap<String, List<String>>();

for (List<String> values : map.values()) {
    for (String value : values) {
        // do what you want with the value here.
    }
}

為了使此循環更短,請看一下LambdaJ。 Jakarta集合也有很多可以簡化此代碼的類。 例如,包裝幾個集合並公開單個集合的API的類。 類似於CollectionsCollection 但是不幸的是,這個庫還不支持泛型。

您應該使用迭代器來遍歷哈希映射。

Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
    // code goes here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM