简体   繁体   中英

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.

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. Jakarta collections also has a lot of classes that may simplify this code. For example class that wraps several collections and exposes API of single collection. Something like 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM