zoukankan      html  css  js  c++  java
  • List和Map常用的几种遍历方式

    遍历一个List有以下几种方法:
    /*//1.普通for循环的方式:
    	List<String> list=new ArrayList<String>();
    	list.add("a");
    	list.add("b");
    	list.add("c");
    	list.add("d");
    	for(int i=0;i<list.size();i++){
    		System.out.print(list.get(i)+",");
    	}*/
    	
    	 /*//2.foreach的方式:
    	 List<String> list=new ArrayList<String>();
    		list.add("a");
    		list.add("b");
    		list.add("c");
    		list.add("d");
    		for(String str:list){
    			System.out.print(str+",");
    		}*/
    	 
    	 /*//3.把链表变为数组再遍历的方式:
    	 List<String> list=new ArrayList<String>();
    		list.add("a");
    		list.add("b");
    		list.add("c");
    		list.add("d");
    		String[] array=new String[list.size()];
    		list.toArray(array);
    		for(String str:array){
    			System.out.print(str+",");
    		}*/
    	
    	 /*// 4.迭代器遍历的方式:
    	 List<String> list=new ArrayList<String>();
    		list.add("a");
    		list.add("b");
    		list.add("c");
    		list.add("d");
    		Iterator< String> iterator=list.iterator();
    		while (iterator.hasNext()) {
    			System.out.print(iterator.next()+",");			
    		}*/
    	 
    

      

    遍历一个List使用迭代器更加线程安全,因为它可以确保,在当前遍历的集合元素被更改的时候,它会抛出ConcurrentModificationException。

      

    遍历map方法有以下几种:
    /*//1.通过Map.keySet遍历的方式:
    	 Map<String,String> map=new HashMap<String,String>();
    	 map.put("1", "a");
    	 map.put("2", "b");
    	 map.put("3", "c");
    	 map.put("4", "d");
    	 Set<String>  keySet=map.keySet();
    	 for(String key:keySet){
    		 System.out.print(key+"="+map.get(key)+",");
    	 }*/
    	 
    	 /*//2.通过Map.entrySet遍历的方式:
    	 Map<String,String> map=new HashMap<String,String>();
    	 map.put("1", "a");
    	 map.put("2", "b");
    	 map.put("3", "c");
    	 map.put("4", "d");
    	 Set<Entry<String,String>> entrySet=map.entrySet();
    	 for(Entry<String,String> entry:entrySet){
    		 System.out.print(entry.getKey()+"="+entry.getValue()+",");
    	 }*/
    	 
    	 /*//3.通过Map.entrySet使用iterator遍历的方式:
    	 Map<String,String> map=new HashMap<String,String>();
    	 map.put("1", "a");
    	 map.put("2", "b");
    	 map.put("3", "c");
    	 map.put("4", "d");
    	 Iterator<Map.Entry<String, String>> iter= map.entrySet().iterator();
    	 while (iter.hasNext()) {
    		 Map.Entry<String, String> entry = iter.next();
    		 System.out.print(entry.getKey()+"="+entry.getValue()+",");
    	}*/
    	 
    	 //4.通过Map.values()遍历所有的value,但不能遍历key的方式:
    	 Map<String,String> map=new HashMap<String,String>();
    	 map.put("1", "a");
    	 map.put("2", "b");
    	 map.put("3", "c");
    	 map.put("4", "d");
    	 for(String value:map.values()){
    		 System.out.print(value+",");
    	 }
    

      

  • 相关阅读:
    jquery实现选项卡(两句即可实现)
    常用特效积累
    jquery学习笔记
    idong常用js总结
    织梦添加幻灯片的方法
    LeetCode "Copy List with Random Pointer"
    LeetCode "Remove Nth Node From End of List"
    LeetCode "Sqrt(x)"
    LeetCode "Construct Binary Tree from Inorder and Postorder Traversal"
    LeetCode "Construct Binary Tree from Preorder and Inorder Traversal"
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6755075.html
Copyright © 2011-2022 走看看