javase中HashMap迭代器的使用

HashMap迭代器有两种使用方法

(1)通过for 循环遍历

HashMap map = new HashMap();
for(String key : map.keySet()){

         map.get(key);
}

(2)通过Map.entrySet用iterator遍历

HashMap hm= new HashMap();

 Set<String> keys=  hm.keySet();//hm.keySet 返回所以的键 ,这样才能加入Iterator进行编译
        Iterator<String> ite =keys.iterator();
        while( ite.hasNext()){
            String key = ite.next();
            hm.get(key);
                       
        }
//ite.hasNext() 返回判断值 ,下一个元素是否有值
//ite.next() 返回下一个值的 Key

 

posted @ 2017-05-02 10:12  BestZsaiwei  阅读(1868)  评论(0编辑  收藏  举报