stream flow traversal java8

 

For comparison loop, iterator, java8Stream traverse different flow

  1 package cnom.test.testUtils;
  2 
  3 import java.io.Serializable;
  4 import java.util.ArrayList;
  5 import java.util.Collections;
  6 import java.util.Comparator;
  7 import java.util.Iterator;
  8 import java.util.List;
  9 import java.util.Random;
 10 
 11 public class TestCollectionsSort {
 12 
 13     public static void main(String[] args) throws InterruptedException {
 14         TestCollectionsSort test = new TestCollectionsSort();
 15 
 16         List<TestSortModel> list = new ArrayList<TestSortModel>();
 17         TestSortModel model = null;
 18         for (int i = 0; i < 8000; i++) {
 19             model = test.new TestSortModel();
 20             int intId = (int) (Math.random() * 100);
 21             model.setIntId(intId);
 22             model.setName("tes" + i);
 23             list.add(model);
 24         }
 25         //        JAVA8 使用Comparator排序
 26         //        list.sort(Comparator.comparing(TestSortModel::getIntId,
 27         //                Comparator.nullsLast(Comparator.naturalOrder())));
 28 
 29         List<String> resList = new ArrayList<String>();
 30 
 31         long t1 = System.currentTimeMillis();
 32         for (TestSortModel tsm : list) {
 33             int id = tsm.getIntId();
 34             //            Thread.sleep(1);
 35             resList.add(tsm.getName());
 36         }
 37         System.out.println("增强for循环 遍历耗时:" + (System.currentTimeMillis() - t1) + ";list.size()="
 38                 + list.size() + ";list=" + resList.toString());
 39         resList.clear();
 40 
 41         long t2 = System.currentTimeMillis();
 42         for (int i = 0; i < list.size(); i++) {
 43             int id = list.get(i).getIntId();
 44             //          Thread.sleep(1);
 45             resList.add(list.get(i).getName());
 46         }
 47         System.out.println("for循环 遍历耗时:" + (System.currentTimeMillis() - t2) + ";list.size()="
 48                 + list.size() + ";list=" + resList.toString());
 49         resList.clear();
 50 
 51         long t3 = System.currentTimeMillis();
 52         Iterator<TestSortModel> it = list.iterator();
 53         TestSortModel tm = null;
 54         while (it.hasNext()) {
 55             tm = it.next();
 56             int= ID tm.getIntId ();
 57 is              //           the Thread.sleep (. 1); 
58              resList.add (tm.getName ());
 59          }
 60          System.out.println ( "Iterator traversal Processed:" + (System. with currentTimeMillis () - T3) + "; list.size () ="
 61 is                  + list.size () + "; List =" + resList.toString ());
 62 is          resList.clear ();
 63 is  
64          // parallerlStream traversal for time-consuming processing business logic. 
65          // Note: If added to another list will traverse the elements, then the list must be thread-safe, otherwise the traversal process, two things occur:
 66          // 1. The elements of the list will be lost situation; 2. traversal will be thrown: ArrayIndexOutOfBoundsException 
67         List <TestSortModel> = listt the Collections.synchronizedList ( new new the ArrayList <TestSortModel> ());
 68          Long T4 = System.currentTimeMillis ();
 69          . List.parallelStream () forEach (TSM -> { // if the original list will remain forEachOrdered sequence 
70              int ID = tsm.getIntId ();
 71 is              //             listt.add (TSM); // output listt elements have lost, the list is not thread-safe 
72              the try {
 73 is                  //                 the Thread.sleep (. 1) ; 
74                  resList.add (tsm.getName ()); // resList output will have lost the element 
75             } The catch (Exception E) {
 76                  e.printStackTrace ();
 77              }
 78          });
 79          the System.out
 80                  .println ( "list.parallelStream () forEach () traversal Processed:." + (System.currentTimeMillis () - T4)
 81                          + "; list.size () =" + resList.size () + "; List =" + resList.toString ());
 82          resList.clear ();
 83  
84          Long T5 = System.currentTimeMillis ( );
 85          . list.stream () forEach (TSM -> { // similar serial execution, and for cycle efficiency, streamlined code 
86             int id = tsm.getIntId();
 87             try {
 88                 //              Thread.sleep(1);
 89                 resList.add(tsm.getName());
 90             } catch (Exception e) {
 91                 // TODO Auto-generated catch block
 92                 e.printStackTrace();
 93             }
 94         });
 95         System.out.println("list.stream().forEach() 遍历耗时:" + (System.currentTimeMillis() - t5)
 96                 + ";list.size()=" + list.size() + ";list=" + resList.toString());
 97         resList.clear ();
 98  
99          Long T6 = System.currentTimeMillis ();
 100          list.forEach (TSM -> { // streamline the code, with similar efficiency for loop 
101              int ID = tsm.getIntId ();
 102              the try {
 103                  //               the Thread.sleep (. 1); 
104                  resList.add (tsm.getName ());
 105              } the catch (Exception E) {
 106                  // the TODO Auto-Generated the catch Block 
107                  e.printStackTrace ();
 108              }
 109          }) ;
 110         System.out.println ( "list.forEach () traversal Processed:" + (System.currentTimeMillis () - T6)
 111                  + "; list.size () =" + list.size () + "; = List" + resList.toString ());
 112          resList.clear ();
 113  
114          / ** 
115           * for Loop1 enhanced for loop iterates Processed: 80051
 1 16           * Processed for loop iterates for Loop2: 80032
 117           * Iterator traversal time: 80069
 118           . * list.parallelStream () forEach () traversal time: more than 20,101 under-threaded environment, the use of better efficiency when performing time-consuming business logic.
119           . * List.stream () forEach () traversal Processed: 80049 streamline the code
 120           * list.forEach () traversal Processed: 80031
 121           * / 
122  
123     }
124 
125     public class TestSortModel implements Serializable {
126 
127         private static final long serialVersionUID = -890909910704287938L;
128 
129         private long longId;
130 
131         private int intId;
132 
133         private String name;
134 
135         public long getLongId() {
136             return longId;
137         }
138 
139         public void setLongId(long longId) {
140             this.longId = longId;
141         }
142 
143         public int getIntId() {
144             return intId;
145         }
146 
147         public void setIntId(int intId) {
148             this.intId = intId;
149         }
150 
151         public String getName() {
152             return name;
153         }
154 
155         public void setName(String name) {
156             this.name = name;
157         }
158 
159     }
160 }
View Code

 

Guess you like

Origin www.cnblogs.com/itfeng813/p/11573179.html