集合类常见用法

hashmap遍历

方法1效率高,iter还可以用泛型来表示比如Iterator

Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    Object key = entry.getKey();
    Object val = entry.getValue();
}

方法2,有2次遍历,其实keyset就执行了一次遍历

Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
    Object key = iter.next();
    Object val = map.get(key);
}

ref

https://www.iteye.com/blog/ludaojuan21-243475

你可能感兴趣的:(集合类常见用法)