比较for循环、迭代器、java8Stream流遍历的不同
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 < 80000; 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 // Thread.sleep(1); 58 resList.add(tm.getName()); 59 } 60 System.out.println("iterator 遍历耗时:" + (System.currentTimeMillis() - t3) + ";list.size()=" 61 + list.size() + ";list=" + resList.toString()); 62 resList.clear(); 63 64 //parallerlStream遍历,适用于处理比较耗时的业务逻辑。 65 //注意:若在遍历中将元素添加到另一个list,则这个list必须是线程安全的,否则遍历过程中会出现两种情况: 66 //1.这个list中的元素会有丢失的情况; 2.遍历中会抛出:ArrayIndexOutOfBoundsException 67 List<TestSortModel> listt = Collections.synchronizedList(new ArrayList<TestSortModel>()); 68 long t4 = System.currentTimeMillis(); 69 list.parallelStream().forEach(tsm -> {//若是 forEachOrdered会保持原来list顺序 70 int id = tsm.getIntId(); 71 // listt.add(tsm);//输出的listt会有元素丢失,线程安全的list则不会 72 try { 73 // Thread.sleep(1); 74 resList.add(tsm.getName());//输出的resList也会有元素丢失 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 }); 79 System.out 80 .println("list.parallelStream().forEach() 遍历耗时:" + (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 -> {//串行执行,与for循环效率相似,代码精简 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 -> {//精简代码,与for循环效率相似 101 int id = tsm.getIntId(); 102 try { 103 // Thread.sleep(1); 104 resList.add(tsm.getName()); 105 } catch (Exception e) { 106 // TODO Auto-generated catch block 107 e.printStackTrace(); 108 } 109 }); 110 System.out.println("list.forEach() 遍历耗时:" + (System.currentTimeMillis() - t6) 111 + ";list.size()=" + list.size() + ";list=" + resList.toString()); 112 resList.clear(); 113 114 /** 115 * for loop1 增强for循环 遍历耗时:80051 116 * for loop2 for循环 遍历耗时:80032 117 * iterator 遍历耗时:80069 118 * list.parallelStream().forEach() 遍历耗时:20101 多线程环境下,执行耗时的业务逻辑时使用效率更好。 119 * list.stream().forEach() 遍历耗时:80049 精简代码 120 * list.forEach() 遍历耗时: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 }