zoukankan      html  css  js  c++  java
  • map和list遍历基础

    本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5456085.html (Robin)

    Map

     1 import java.util.HashMap;
     2 import java.util.Iterator;
     3 import java.util.Map;
     4 
     5 /**
     6  * Created by robin on 2016/4/12.
     7  *
     8  * @author robin
     9  */
    10 public class MapTest {
    11 
    12     public static void main(String args[]){
    13         traversalKey();
    14     }
    15 
    16     /**
    17      * 在不知道map keys 的情况下 遍历map的key和value
    18      *
    19      * 总结
    20      *  1.仅需要键(keys) map.keySet()
    21      *  2.仅需要值(values) map.values()
    22      *  3.使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三
    23      *  4.键值都要使用方法二。
    24      */
    25     public static void traversalKey(){
    26         Map<String,String> map = new HashMap<String, String>();
    27         map.put("1","a");
    28         map.put("2","b");
    29         map.put("3","c");
    30         map.put("4","d");
    31         map.put("5","e");
    32         map.put("6","e");
    33         /**第一种方法:keySet() 获取key值,通过key值get Value 效率最低!由key get value 耗时*/
    34         for (String key:map.keySet()){
    35             System.out.println("key:"+key+"--value:"+map.get(key));
    36         }
    37         /**第二种方法:Map.Entry 效率高于第一种,一般推荐这种写法*/
    38         for(Map.Entry<String,String> entry:map.entrySet()){
    39             System.out.println("key:"+entry.getKey()+"--value:"+entry.getValue());
    40         }
    41        /**第三种方法:Iterator遍历 这种遍历方法新旧java版本皆通用,在遍历删除map的key-value时,这是唯一可选择的方法*/
    42         Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
    43         while (entries.hasNext()) {
    44             Map.Entry<String, String> entry = entries.next();
    45             System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    46         }
    47         /**扩展 直接获取map的所有value值*/
    48         for(String value:map.values()){
    49             System.out.println(value);
    50         }
    51 
    52     }
    53 }

    List

      1 import java.util.ArrayList;
      2 import java.util.Iterator;
      3 import java.util.List;
      4 
      5 /**
      6  * Created by robin on 2016/4/13.
      7  * 关于list遍历有三种办法:①普通方法;②增强for循环;③iterator
      8  * 效率 ①>③>②
      9  *
     10  * ①内部不锁定, 效率最高, 但是当写多线程时要考虑并发操作的问题
     11  * ②内部实现是使用了③
     12  * ③执行过程中会进行数据锁定, 性能稍差
     13  *
     14  * list遍历删除数据建议采用方法③,若采用方法①,请使用倒序遍历删除,详情参考:ordinaryDelDesc;
     15  * 不可使用②进行遍历删除,会产生bug
     16  * @author robin
     17  */
     18 public class ListTest {
     19 
     20     public static void main(String args[]){
     21         List<Student> stuList = initList();
     22         /**
     23          * 普通遍历方法
     24          */
     25         for(int i= 0;i< stuList.size();i++){
     26             Student stuShow = stuList.get(i);
     27 //            System.out.println("stuId:"+stuShow.getStuId()+"----stuName:"+stuShow.getName());
     28         }
     29 
     30         System.out.println("----普通遍历方法----");
     31 
     32         /**
     33          * 增强for循环遍历
     34          */
     35         for (Student stu:stuList){
     36 //            System.out.println("stuId:"+stu.getStuId()+"----stuName:"+stu.getName());
     37         }
     38         System.out.println("----增强for循环遍历----");
     39         /**
     40          * Iterator 遍历
     41          */
     42         Iterator<Student> iterator = stuList.iterator();
     43         while (iterator.hasNext()){
     44             Student stuShow = iterator.next();
     45 //            System.out.println("stuId:"+stuShow.getStuId()+"----stuName:"+stuShow.getName());
     46         }
     47         System.out.println("----Iterator循环遍历----");
     48 
     49         //list中删除元素方法
     50         ordinaryDelAsc(initList());
     51         System.out.println("----普通遍历正序删除----");
     52         ordinaryDelDesc(initList());
     53         System.out.println("----普通遍历倒序删除----");
     54 //        heighten(stuList);
     55         iteratorDel(initList());
     56         System.out.println("----Iterator循环遍历删除----");
     57     }
     58 
     59     public static List<Student> initList(){
     60         List<Student> stuList = new ArrayList<Student>();
     61         Student stuA = new Student("10001","路飞");
     62         stuList.add(stuA);
     63         Student stuB = new Student("10002","索隆");
     64         stuList.add(stuB);
     65         Student stuC = new Student("10003","山治");
     66         stuList.add(stuC);
     67         Student stuD = new Student("10004","娜美");
     68         stuList.add(stuD);
     69         Student stuE = new Student("10005","罗宾");
     70         stuList.add(stuE);
     71         Student stuF = new Student("10006","乌索普--1号");
     72         stuList.add(stuF);
     73         Student stuJ = new Student("10007","弗兰奇");
     74         stuList.add(stuJ);
     75         Student stuH = new Student("10006","乌索普--2号");
     76         stuList.add(stuH);
     77         Student stuK = new Student("10008","乔巴");
     78         stuList.add(stuK);
     79         Student stuI = new Student("10006","乌索普--3号");
     80         stuList.add(stuI);
     81         Student stuM = new Student("10006","乌索普--4号");
     82         stuList.add(stuM);
     83         Student stuL = new Student("10009","布鲁克");
     84         stuList.add(stuL);
     85         return stuList;
     86     }
     87 
     88     /**
     89      * 普通遍历方法 正序 删除预期数据
     90      * 这种方法存在bug:删除元素时涉及到数组元素的移动,遍历下标n的元素。
     91      * 当前元素符合条件,执行删除操作,当前n位置的元素被删除,n+1位置的元素,移动到n位置上。
     92      * 搜索光标却还是n,并未n-1,下次循环搜索时,会从下标n+1开始检索。
     93      * 此时n+1位置的元素,原本是n+2的元素,那么此次循环遍历检索就会遗漏n+1位置的元素检查。
     94      * 倘若n+1位置的元素符合删除条件,那么程序就将出现bug。
     95      * 因为建议多用iterator进行遍历删除,如果非得使用普通循环遍历方法删除元素,请采用倒序的办法,详见ordinaryDesc
     96      * @param stuList
     97      */
     98     public static void ordinaryDelAsc(List<Student> stuList){
     99         for(int i= 0;i< stuList.size();i++){
    100             Student stuShow = stuList.get(i);
    101             if(stuShow.getStuId().equals("10006")){
    102                 stuList.remove(stuShow);
    103             }
    104         }
    105         System.out.println(listToString(stuList));
    106     }
    107     /**
    108      * 普通遍历方法 倒序 删除预期数据
    109      * @param stuList
    110      */
    111     public static void ordinaryDelDesc(List<Student> stuList){
    112         for(int i= stuList.size()-1;i>0 ;i--){
    113             Student stuShow = stuList.get(i);
    114             if(stuShow.getStuId().equals("10006")){
    115                 stuList.remove(stuShow);
    116             }
    117         }
    118         System.out.println(listToString(stuList));
    119     }
    120 
    121     /**
    122      * 增强for循环遍历删除预期数据
    123      * @param stuList
    124      */
    125     public static void heighten(List<Student> stuList){
    126         for (Student stu:stuList){
    127             if(stu.getStuId().equals("10006")){
    128                 stuList.remove(stu);
    129             }
    130         }
    131         System.out.println(listToString(stuList));
    132     }
    133 
    134     /**
    135      * Iterator 循环遍历删除预期数据
    136      * @param stuList
    137      * @return
    138      */
    139     public static void iteratorDel(List<Student> stuList){
    140         Iterator<Student> iterator = stuList.iterator();
    141         while (iterator.hasNext()){
    142             Student stuShow = iterator.next();
    143             if(stuShow.getStuId().equals("10006")){
    144                 iterator.remove();
    145             }
    146         }
    147         System.out.println(listToString(stuList));
    148     }
    149 
    150     private static String listToString(List<Student> list){
    151         StringBuilder sb = new StringBuilder("");
    152         for (Student s:list){
    153             sb.append("[stuId:"+s.getStuId()+"---name:"+s.getName()+"] 
    ");
    154         }
    155         return sb.toString();
    156     }
    157 
    158 
    159 }
    160 
    161 class Student{
    162 
    163     public Student(String stuId,String name){
    164         this.stuId =stuId;
    165         this.name = name;
    166     }
    167 
    168     private String stuId;
    169 
    170     private String name;
    171 
    172     public void setStuId(String stuId){
    173         this.stuId = stuId;
    174     }
    175 
    176     public String getStuId(){
    177         return  this.stuId;
    178     }
    179 
    180     public void setName(String name){
    181         this.name = name;
    182     }
    183 
    184     public String getName(){
    185         return this.name;
    186     }
    187 }
  • 相关阅读:
    Emacs和ESS的使用技巧。
    响应式
    Day learn,day up
    Docker快速安装kafka | 沈健的技术博客
    闭包函数如何使用循环变量
    leetcode笔记——35.搜索插入位置
    CSS 之动态变换背景颜色
    吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据2
    吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据
    吴裕雄 PYTHON 神经网络——TENSORFLOW 单隐藏层自编码器设计处理MNIST手写数字数据集并使用TensorBord描绘神经网络数据
  • 原文地址:https://www.cnblogs.com/robinjava77/p/5456085.html
Copyright © 2011-2022 走看看