Talking about the Collection Framework 4 - Collection Extension: Efficiency Comparison of Collection Loop Output Mode and List Output Mode

I just finished learning the collection framework recently, and I want to organize some of my study notes and ideas, so this blog may have some content that is not rigorous or incorrect, please point out. Beginners only recommend this blog as a reference, welcome to leave a message to learn together.

  The system concept of the collection framework was introduced before ( http://www.cnblogs.com/yjboke/p/8761195.html ), this article introduces the collection extension content: the efficiency comparison between the collection loop output method and the list output method


 

Traverse loop output mode.

  I will introduce four traversal output methods here, for loop, iterator (Iterator), enhanced for loop (foreach), and ListIterator (ListIterator) specific to List collections.

  The enhanced for loop simplifies the code on the basis of the for loop. The ordinary for loop may not have a traversal target, while the enhanced for loop must have a traversal target. In the enhanced for loop, operations such as additions, deletions, and modifications are not allowed. For operations such as additions, deletions, and modifications, please use the ordinary for loop.

  ListIterator is a sub-interface of Iterator, which makes up for its shortcomings that it cannot be added or modified. 

  List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        
        // Ordinary for loop output 
        for ( int i = 0; i<list.size(); i++ ){
            System.out.println(list.get(i));
        }
        
        // Enhanced for loop output 
        for (String s : list){
            System.out.println(s);
        }
        // List iterator 
        ListIterator<String> li = list.listIterator();
         while (li.hasNext()){
            Object object = li.next();
            if (object.equals("abc3")) {
                li.add("asd");
            }
        }
        System.out.println(list);
        // Iterator output 
        for (Iterator<String> it = list.iterator(); it.hasNext();){
            System.out.println(it.next());
        }

  The Map collection does not implement the Iterable interface, so the map collection cannot directly use the enhanced for loop. If you need to use the enhanced for loop, you need to use the keySet or entrySet collection of the Set collection.

  Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1001, "zhangsan");                        
        map.put(1002, "wangwu");
        map.put(1003, "lisi");
        map.put(1004, "maliu");
        //keySet方法
        Set<Integer> keySet = map.keySet();
        for(Integer i : keySet){
            System.out.println("key:"+i+"; value:"+map.get(i));
        }
        //entrySet方法
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        for(Map.Entry<Integer, String> i : entrySet){
            System.out.println("key:" + i.getKey() + "----value:"+i.getValue());
        }

 ——————————————————————————————————————————————————————————————————————

Next, add some changes to the above code, define a set with a length of 20000000, use the above four methods to traverse the output and record the time they consume, and compare the efficiency of the output.

 

 1 public static void main(String[] args) {
 2         list();
 3     }
 4     
 5     public static void list() {
 6         List<String> list = new ArrayList<String>();
 7         //定义一个集合的长度
 8         int num = 20000000;
 9         long timeStart,timeEnd;
10         for (int i = 0; i < num; i++) {
11             list.add("abd"+i);
12         }
13         
14         // Test the time used by the foreach loop 
15          timeStart = System.currentTimeMillis();
 16          for (String s : list) {
 17              
18          }
 19          timeEnd = System.currentTimeMillis();
 20          System.out.println("The foreach loop time is : " + (timeEnd-timeStart) + "ms" );
 21          
22          // Test the time used by the Iterator loop 
23          timeStart = System.currentTimeMillis();
 24          Iterator<String> it = list.iterator();
 25          while (it .hasNext()) {
 26 it. next              ();
 27         }
 28          timeEnd = System.currentTimeMillis();
 29          System.out.println("Iterator loop time is: " + (timeEnd-timeStart) + "ms" );
 30          
31          // The time used to test the Iterator loop 
32          timeStart = System .currentTimeMillis();
 33          ListIterator<String> lit = list.listIterator();
 34          while (lit.hasNext()) {
 35              lit.next();
 36          }
 37          timeEnd = System.currentTimeMillis();
 38          System.out .println("ListIterator loop time is: " + (timeEnd-timeStart) + "ms" );
39          
40          // Test the time used by the for loop 
41          timeStart = System.currentTimeMillis();
 42          for ( int i = 0; i<list.size(); i++ ) {
 43              list.get(i);
 44          }
 45          timeEnd = System.currentTimeMillis();
 46          System.out.println("The for loop time is: " + (timeEnd-timeStart) + "ms" );
 47      }

  The output is:

The foreach loop time is: 143ms

Iterator loop time is: 12ms

ListIterator loop time is: 13ms

The for loop time is: 18ms

 

 Then the four looping methods are output separately, and the output results are:

The foreach loop time is: 103ms
Iterator loop time is: 9ms
ListIterator loop time is: 10ms
The for loop time is: 17ms

 It can be seen that Iterator is similar to ListIterator, the for loop is slightly slower, and the foreach is the slowest. (If the test code is not rigorous, please point it out and learn together.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645898&siteId=291194637