遍历key+value
- keySet遍历key+value(写法1):
Iterator<String> it= map.keySet().iterator(); while (iter.hasNext()) { key = it.next(); value = map.get(key); } |
- keySet遍历key+value(写法2):
for (String key : map.keySet()) { value = map.get(key); } |
- entrySet遍历key+value(写法1):
Iterator<Entry<String, String>> it= map.entrySet().iterator(); Entry<String, String> entry; while (it.hasNext()) { entry = it.next(); key = entry.getKey(); value = entry.getValue(); } |
- entrySet遍历key+value(写法2):
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); } |
遍历key
- keySet遍历key(写法1):
Iterator<String> it= map.keySet().iterator(); while (iter.hasNext()) { key = it.next(); } |
- keySet遍历key(写法2):
for (String key : map.keySet()) { } |
- entrySet遍历key(写法1):
Iterator<Entry<String, String>> it= map.entrySet().iterator(); while (it.hasNext()) { key = iter.next().getKey(); } |
- entrySet遍历key(写法2):
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); } |
遍历value
- keySet遍历value(写法1):
Iterator<String> it= map.keySet().iterator(); while (it.hasNext()) { value = map.get(iter.next()); } |
- keySet遍历value(写法2):
for (String key : map.keySet()) { value = map.get(key); } |
- entrySet遍历value(写法1):
Iterator<Entry<String, String>> it= map.entrySet().iterator(); while (it.hasNext()) { value = it.next().getValue(); } |
- entrySet遍历value(写法2):
for (Entry<String, String> entry: map.entrySet()) { value = entry.getValue(); } |
- values遍历value(写法1):
Iterator<String> it= map.values().iterator(); while (it.hasNext()) { value = it.next(); } |
- values遍历value(写法2):
for (String value : map.values()) { } |