javase中HashMap迭代器的使用

it2022-05-09  17

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

 

转载于:https://www.cnblogs.com/BestZsaiwei/p/6794843.html


最新回复(0)