简体   繁体   English

HashMap上的数组迭代

[英]Iteration of arrays over a HashMap

is there a simple method to iterate all Arrays which are in one Hashmap? 有没有一种简单的方法来迭代一个哈希图中的所有数组?

For example: 例如:

 HashMap<String, ArrayList<String>>

i search a element from a array which is in one of the HashValue. 我从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.
    }
}

To make this loop shorter take a look on LambdaJ. 为了使此循环更短,请看一下LambdaJ。 Jakarta collections also has a lot of classes that may simplify this code. Jakarta集合也有很多可以简化此代码的类。 For example class that wraps several collections and exposes API of single collection. 例如,包装几个集合并公开单个集合的API的类。 Something like CollectionsCollection . 类似于CollectionsCollection But unfortunately this library does not support generics yet. 但是不幸的是,这个库还不支持泛型。

You should use an iterator to run through hashmaps. 您应该使用迭代器来遍历哈希映射。

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