If you're only interested in the keys, you can iterate through the keySet()
of the map:
(如果您仅对键感兴趣,则可以遍历地图的keySet()
:)
Map<String, Object> map = ...;
for (String key : map.keySet()) {
// ...
}
If you only need the values, use values()
:
(如果只需要这些值,请使用values()
:)
for (Object value : map.values()) {
// ...
}
Finally, if you want both the key and value, use entrySet()
:
(最后,如果您想要键和值,请使用entrySet()
:)
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator (see karim79's answer ).
(一个警告:如果要在迭代中删除项目,则需要通过Iterator进行删除(请参阅karim79的答案 )。)
However, changing item values is OK (see Map.Entry
). (但是,更改项目值是可以的(请参见Map.Entry
)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…