我是靠谱客的博主 犹豫太阳,这篇文章主要介绍Java中ArrayList与HashMap的遍历,现在分享给大家,希望可以做个参考。

1.ArrayList遍历

[html]  view plain copy print ?
  1. package com.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6. public class ArrayListDemo {  
  7.     public static void main(String args[]){  
  8.       List<String> list = new ArrayList<String>();  
  9.       list.add("luojiahui");  
  10.       list.add("luojiafeng");  
  11.   
  12.       //方法1  
  13.       Iterator it1 = list.iterator();  
  14.       while(it1.hasNext()){  
  15.           System.out.println(it1.next());  
  16.       }  
  17.   
  18.       //方法2  
  19.       for(Iterator it2 = list.iterator();it2.hasNext();){  
  20.           System.out.println(it2.next());  
  21.       }  
  22.   
  23.       //方法3  
  24.       for(String tmp:list){  
  25.           System.out.println(tmp);  
  26.       }  
  27.   
  28.       //方法4  
  29.       for(int i = 0;i < list.size(); i ++){  
  30.           System.out.println(list.get(i));  
  31.       }  
  32.   }  
  33.   
  34. }  

2.HashMap的遍历

[html]  view plain copy print ?
  1. 方法1:  
  2. HashMap staff = new HashMap();   
  3. Set entries = staff.entrySet();   
  4. Iterator iter = entries.iterator();   
  5. while(iter.hasNext())   
  6. {   
  7.       Map.Entry entry = (Map.Entry)iter.next();   
  8.       Object key = entry.getKey();   
  9.       Object value = entry.getValue();   
  10. }  
  11.   
  12. 方法2:  
  13. Map map = new HashMap();  
  14. for(Iterator iter = map.entrySet().iterator(); iter.hasNext();) {  
  15.     Map.Entry entry = (Map.Entry) iter.next();    //map.entry 同时取出键值对  
  16.     Object key = entry.getKey();  
  17.     Object val = entry.getValue();  
  18. }  
  19.   
  20. 方法3:  
  21. Map map = new HashMap();  
  22. for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {  
  23.     Object key = iter.next();  
  24.     Object val = map.get(key);  
  25. }  


----------------------------------------------------------

HashMap遍历的补充

PS:如果要保持HashMap的遍历顺序和原插入顺序一致,可以使用LinkedHashMap,使用方法和HashMap一样,改一下声明即可:LinkedHashMap myMap = new LinkedHashMap(); 当然需要导入:java.util.LinkedHashMap

[html]  view plain copy print ?
  1. import java.util.Collection;  
  2. import java.util.HashMap;  
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. public class MapList {  
  6. /**  
  7.   * @param args  
  8.   */  
  9. public static void main(String[] args) {  
  10.   // TODO Auto-generated method stub  
  11.   HashMap myMap = new HashMap();  
  12.   myMap.put("hello", "你好");  
  13.   myMap.put("bye", "再见");  
  14.   myMap.put("thanks", "谢谢");  
  15.   myMap.put("ok", "好的");  
  16.   System.out.println("--------------------遍历key和value----------------------");  
  17.   for(Iterator iter = myMap.entrySet().iterator();iter.hasNext();){  
  18.             Map.Entry element = (Map.Entry)iter.next();  
  19.             Object strKey = element.getKey();  
  20.             Object strObj = element.getValue();  
  21.             System.out.println("myMap.get(""+strKey+"")="+strObj);  
  22.   }  
  23.   System.out.println();  
  24.   System.out.println("--------------------遍历整个HashMap----------------------");  
  25.   Collection objs = myMap.entrySet();  
  26.   for (Iterator iterator=objs.iterator(); iterator.hasNext();){  
  27.    Object obj = iterator.next();  
  28.    System.out.println(obj);  
  29.   }  
  30.   System.out.println();  
  31.   System.out.println("--------------------遍历HashMap的key----------------------");  
  32.   Collection keys = myMap.keySet();  
  33.   for (Iterator iterator=keys.iterator(); iterator.hasNext();){  
  34.    Object key = iterator.next();  
  35.    System.out.println(key);  
  36.   }  
  37.   System.out.println();  
  38.   System.out.println("--------------------遍历HashMap的value----------------------");  
  39.   Collection values = myMap.values();  
  40.   for (Iterator iterator=values.iterator(); iterator.hasNext();){  
  41.    Object value = iterator.next();  
  42.    System.out.println(value);  
  43.   }  
  44. }  
  45. }   

运行结果: 

[html]  view plain copy print ?
  1. --------------------遍历key和value----------------------  
  2. myMap.get("hello")=你好  
  3. myMap.get("thanks")=谢谢  
  4. myMap.get("ok")=好的  
  5. myMap.get("bye")=再见  
  6. --------------------遍历整个HashMap----------------------  
  7. hello=你好  
  8. thanks=谢谢  
  9. ok=好的  
  10. bye=再见  
  11. --------------------遍历HashMap的key----------------------  
  12. hello  
  13. thanks  
  14. ok  
  15. bye  
  16. --------------------遍历HashMap的value----------------------  
  17. 你好  
  18. 谢谢  
  19. 好的  
  20. 再见  

=================================================

本文转自多处,如有侵权请告知,本人将立即删除本文!

最后

以上就是犹豫太阳最近收集整理的关于Java中ArrayList与HashMap的遍历的全部内容,更多相关Java中ArrayList与HashMap内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部