Preface

The traversal method of nested map set is still keySet and entrySet, one by one

case

  //Define a collection to save the student ID and name of Java EE students
  //Define a set to save the student number and name of Ui
  //Define a set to save the student number and name of the web
   Map<String, String> javaEE = new LinkedHashMap<String, String>();
        javaEE.put("001", "Xiao Ming");
        javaEE.put("002", "Xiaohong");
        javaEE.put("003", "Little green");
        Map<String, String> uiStud = new LinkedHashMap<String, String>();
        uiStud.put("004", "Zhang San");
        uiStud.put("005", "Li Si");
        uiStud.put("006", "Wang Wu");
        Map<String, String> webStud = new LinkedHashMap<String, String>();
        webStud.put("007", "February red");
        webStud.put("008", "Half mile");
        webStud.put("009", "Wu Lao Gu");
        //Because this is a nested map set, one of the student s' generics is map < string, string >
        Map<String, Map<String, String>> student = new LinkedHashMap<String, Map<String, String>>();
        student.put("JavaEE", javaEE);
        student.put("Ui", uiStud);
        student.put("Web", webStud);

Traversing a set with a keySet

 //1. The key value returned by traversing with key set is set set
        Set<String> subject=student.keySet();
 //2. Use iterator
        Iterator<String> it=subject.iterator();
        while(it.hasNext()){
            String key=it.next();
            //According to the key value, the map set is returned.
            Map<String,String> stu=student.get(key);
            System.out.println(key+"  "+stu);
        }

Traversing a set with entrySet

  //3. Use entrySet to get each (key, value) set
        Set<Map.Entry<String,Map<String,String>>> entrys=student.entrySet();
 //4. Use iterator to traverse entries
        Iterator<Map.Entry<String,Map<String,String>>> it1=entrys.iterator();
        while (it1.hasNext()){
            //The return value of each item traversed is map. Entry < string, map < string, string > >
            Map.Entry<String,Map<String,String>> stud=it1.next();
            //Traversed key value
            String key=stud.getKey();
            //The value value traversed returns the Map set
            Map<String,String> vale=stud.getValue();
            //Traverse the collection of various disciplines and get the student number and name.
            Set<String> studen=vale.keySet();
            for (String va:studen) {
                System.out.println("subject="+key+"Student ID"+va+"Full name"+vale.get(va));
            }
        }

Comparison between keySey traversal and entrySet traversal

//Build a map set
       HashMap<String,String> map1=new HashMap<String, String>();
        for (int i = 0; i <10000000 ; i++) {
            map1.put("001"+i,"sd");
        }
        //1. Traverse with keyset
        long start= System.currentTimeMillis();
        Set<String> getKey=map1.keySet();
        Iterator<String> it=getKey.iterator();
        while (it.hasNext())
        {
            String value=map1.get(it.next());
        }
        System.out.println("Use keyset Ergodic cost"+(System.currentTimeMillis()-start));
        //2. Traverse with entrySet
        long start1= System.currentTimeMillis();
        Set<Map.Entry<String,String>> entry=map1.entrySet();
        Iterator<Map.Entry<String,String>> it1=entry.iterator();
        while (it1.hasNext())
        {
            Map.Entry<String,String> map2=it1.next();
            String key=map2.getKey();
            String value=map2.getValue();
        }
        System.out.println("Use entrySet Ergodic cost"+(System.currentTimeMillis()-start1));

Here we output the time-consuming of two traversal methods.

summary

  • keySet traversal method: get all key values, and then get value values according to key values
  • entrySet traversal method: traverse each item in Map set, use getkey() to get key value, getvalue() to get value value value
  • After comparison, the ergodic performance of entrySet is better than that of keySet, so more entry is used for ergodic Map set.