Java Map遍历value的6种方法

1. keySet迭代遍历value

Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
    value = map.get(iter.next());
}

2. keySet用for循环遍历value

for (String key : map.keySet()) {
    value = map.get(key);
}

3. entrySet迭代遍历value

Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
 value = iter.next().getValue();
}

4. entrySet用for循环遍历value

for (Entry<String, String> entry: map.entrySet()) {
    value = entry.getValue();
}

5. values迭代遍历value

Iterator<String> iter = map.values().iterator();
while (iter.hasNext()) {
 value = iter.next();
}

6. values用for循环遍历value

for (String value : map.values()) {

}



版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。