Java 遍历 HashMap 的三种方式

@bingoogolapple 2016-12-08 07:06:50发表于 bingoogolapple/bingoogolapple.github.io Java

方法一:键值都需要遍历的情况

for (Map.Entry<String, BGAImageFolderModel> imageFolderModelEntry : imageFolderModelMap.entrySet()) {
    imageFolderModelEntry.getKey();
    imageFolderModelEntry.getValue();
}

方法二:仅需要遍历键或者值的情况

for (BGAImageFolderModel imageFolderModel : imageFolderModelMap.values()) {
}

for (String folderPath : imageFolderModelMap.keySet()) {
}

方法三:边遍历边删除的情况

Iterator<Map.Entry<String, BGAImageFolderModel>> iterator = imageFolderModelMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, BGAImageFolderModel> entry = iterator.next();
    entry.getKey();
    entry.getValue();
    iterator.remove();
}