Java1.8中的Collections類的功能加強不少,你能夠直接調用串行的Collections.stream()或並行的Collections.parallelStream()來對List,Set中的數據進行處理與操做(不支持Map)。java
1:stream是什麼?apache
java.util.Stream是一個接口,是1.8引入。它表示了某原子的序列,能夠對原子進行各類操做,Stream能夠是中間操做,也能夠作爲最重操做。做爲中間操做時返回流對象自己,做爲最重操做時返回某種類型的值。做爲中間操做時能夠在這個流上屢次調用操做方法。dom
2:lambda表達式ide
其實lambda表達式實質上是個匿名函數,它由三部分組成:參數列表,箭頭(->),以及一個表達式或語句塊。來看一個示例:函數
public void sum(int a, int b){ return a+b; }
轉爲 lambda表達式爲ui
(int a, int b) -> a + b;
還能夠這樣寫,由於java會根據上下文推斷出參數類型線程
(a, b) -> {return a + b;}
或code
(a, b) -> a + b;
3:具體示例orm
package cn.huiyunche.driver.controller; import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; public class Lambda { private static List<String> lambdaList = new ArrayList<>(); private static String string = "string"; private static String integer = "integer"; public Lambda() { initData(string); } public static void initData(String flag) { if (CollectionUtils.isNotEmpty(lambdaList)) { lambdaList.clear(); } if (string.equals(flag)) { lambdaList.add("Ulrica"); lambdaList.add("Quella"); lambdaList.add("Cecilia"); lambdaList.add("Claudia"); lambdaList.add("Desdemona"); lambdaList.add("Indira"); } else { lambdaList.add("1"); lambdaList.add("2"); lambdaList.add("2"); lambdaList.add("3"); lambdaList.add("3"); lambdaList.add("4"); lambdaList.add("4"); lambdaList.add("4"); lambdaList.add("5"); lambdaList.add("5"); lambdaList.add("5"); lambdaList.add("6"); lambdaList.add("6"); } } // 這種方式就很少講了,之前舊版本比較常見的作法 public static void runThreadUseInnerClass() { new Thread(new Runnable() { @Override public void run() { System.out.println("內部類實現的線程"); } }).start(); } // 新版本寫法 public static void thred() { new Thread(() -> System.out.println("內部類實現的線程")).start(); } // 遍歷list public static void ergodicList() { initData(string); System.out.println("=================遍歷list開始==========>>>>>>>>>>>"); lambdaList.forEach((value) -> System.out.println("value = " + value)); System.out.println("<<<<<<<<<<<======遍歷list結束====================="); } // 正序排序list public static void asc() { initData(integer); System.out.println("=================正序排序list開始==========>>>>>>>>>>>"); lambdaList.stream().sorted().forEach((value) -> System.out.println("value = " + value)); System.out.println("=================正序排序list結束==========>>>>>>>>>>>"); } // 倒序排序list public static void desc() { initData(integer); System.out.println("=================倒序排序list開始==========>>>>>>>>>>>"); lambdaList.stream().sorted((a, b) -> b.compareTo(a)).forEach((value) -> System.out.println("value = " + value)); System.out.println("=================倒序排序list結束==========>>>>>>>>>>>"); } // 過濾 public static void filter(String val) { initData(string); System.out.println("=================過濾開始==========>>>>>>>>>>>"); lambdaList.stream().filter((value) -> value.toLowerCase().contains(val.toLowerCase())).forEach((value) -> System.out.println("value = " + value)); System.out.println("=================過濾結束==========>>>>>>>>>>>"); } // map使用 public static void map() { initData(string); System.out.println("=================map使用開始==========>>>>>>>>>>>"); //lambdaList.stream().map((value) -> value.toUpperCase()).forEach((value) -> System.out.println("value = " + value)); lambdaList.stream().map(String::toUpperCase).forEach((value) -> System.out.println("value = " + value)); System.out.println("=================map使用結束==========>>>>>>>>>>>"); } //parallelStream (並行) 和 stream(串行) public static void stream() { System.out.println("=================parallelStream (並行) 和 stream(串行)開始==========>>>>>>>>>>>"); int max = 1000000; long t0, t1, count, millis; List<String> list = new ArrayList<>(max); for (int i = 0; i < max; i++) { UUID uuid = UUID.randomUUID(); list.add(uuid.toString()); } // 串行 t0 = System.nanoTime(); count = list.stream().sorted().count(); System.out.println("count = " + count); t1 = System.nanoTime(); millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("串行排序耗時: %d ms", millis)); System.out.println("==========================="); long t00, t11, count0, millis0; List<String> list0 = new ArrayList<>(max); for (int i = 0; i < max; i++) { UUID uuid = UUID.randomUUID(); list0.add(uuid.toString()); } // 串行 t00 = System.nanoTime(); count0 = list0.stream().sorted().count(); System.out.println("count0 = " + count0); t11 = System.nanoTime(); millis0 = TimeUnit.NANOSECONDS.toMillis(t11 - t00); System.out.println(String.format("並行排序耗時: %d ms", millis0)); System.out.println("==========================="); long millis00; millis00 = TimeUnit.NANOSECONDS.toMillis((t1 - t0) - (t11 - t00)); System.out.println(String.format("串行比並行排序多耗時: %d ms", millis00)); System.out.println("=================parallelStream (並行) 和 stream(串行)結束==========>>>>>>>>>>>"); } // noneMatch public static void noneMatch(String val) { System.out.println("=================noneMatch使用開始==========>>>>>>>>>>>"); initData(string); boolean bool = lambdaList.stream().noneMatch((value) -> value.contains(val)); System.out.println("contains bool = " + bool); bool = lambdaList.stream().noneMatch((value) -> value.endsWith(val)); System.out.println("endsWith bool = " + bool); bool = lambdaList.stream().noneMatch((value) -> value.startsWith(val)); System.out.println("startsWith bool = " + bool); System.out.println("=================noneMatch使用結束==========>>>>>>>>>>>"); } // allMatch public static void allMatch(String val) { System.out.println("=================allMatch使用開始==========>>>>>>>>>>>"); initData(string); boolean bool = lambdaList.stream().allMatch((value) -> value.startsWith(val)); System.out.println("bool = " + bool); System.out.println("=================allMatch使用結束==========>>>>>>>>>>>"); } // anyMatch public static void anyMatch(String val) { System.out.println("=================anyMatch使用開始==========>>>>>>>>>>>"); initData(string); boolean bool = lambdaList.stream().anyMatch((value) -> value.matches(val)); System.out.println("bool = " + bool); System.out.println("=================anyMatch使用結束==========>>>>>>>>>>>"); } // collect(過濾數據,返回List) public static void collect(String val) { System.out.println("=================collect使用開始==========>>>>>>>>>>>"); initData(string); List<String> lists = lambdaList.stream().filter((value) -> value.toLowerCase().contains(val.toLowerCase())).collect(Collectors.toList()); System.out.println(String.format("lists.size: %d ", lists.size())); lists.forEach((value) -> System.out.println("value = " + value)); System.out.println("=================collect使用結束==========>>>>>>>>>>>"); } // reduce(拼接數據只返回一個結果集) public static void reduce() { System.out.println("=================reduce使用開始==========>>>>>>>>>>>"); initData(string); lambdaList.stream().reduce((value1, value2) -> value1 + value2).ifPresent(System.out::println); System.out.println("=================reduce使用結束==========>>>>>>>>>>>"); } // 統計大於2小於5 public static void groupBysum() { System.out.println("=================統計大於2小於5使用開始==========>>>>>>>>>>>"); initData(integer); lambdaList.parallelStream().map(Integer::new).filter(val -> val >= 2 && val <= 6).collect(Collectors.groupingBy(p -> new Integer(p), Collectors.summingInt(p -> p))).forEach((key, value) -> System.out.println("key = " + key + " value = " +value)); System.out.println("=================統計大於2小於5使用結束==========>>>>>>>>>>>"); } public static void main(String[] args) { thred(); runThreadUseInnerClass(); ergodicList(); asc(); desc(); filter("c"); map(); stream(); noneMatch("Claudia"); allMatch("U"); anyMatch("Claudia"); collect("c"); reduce(); groupBysum(); } }
輸出結果對象
內部類實現的線程 內部類實現的線程 =================遍歷list開始==========>>>>>>>>>>> value = Ulrica value = Quella value = Cecilia value = Claudia value = Desdemona value = Indira <<<<<<<<<<<======遍歷list結束===================== =================正序排序list開始==========>>>>>>>>>>> value = 1 value = 2 value = 3 value = 4 value = 4 value = 4 value = 5 value = 6 =================正序排序list結束==========>>>>>>>>>>> =================倒序排序list開始==========>>>>>>>>>>> value = 6 value = 6 value = 5 value = 4 value = 3 value = 2 value = 1 =================倒序排序list結束==========>>>>>>>>>>> =================過濾開始==========>>>>>>>>>>> value = Ulrica value = Cecilia value = Claudia =================過濾結束==========>>>>>>>>>>> =================map使用開始==========>>>>>>>>>>> value = ULRICA value = QUELLA value = CECILIA value = CLAUDIA value = DESDEMONA value = INDIRA =================map使用結束==========>>>>>>>>>>> =================parallelStream (並行) 和 stream(串行)開始==========>>>>>>>>>>> count = 1000000 串行排序耗時: 826 ms =========================== count0 = 1000000 並行排序耗時: 781 ms =========================== 串行比並行排序多耗時: 44 ms =================parallelStream (並行) 和 stream(串行)結束==========>>>>>>>>>>> =================noneMatch使用開始==========>>>>>>>>>>> contains bool = false endsWith bool = false startsWith bool = false =================noneMatch使用結束==========>>>>>>>>>>> =================allMatch使用開始==========>>>>>>>>>>> bool = false =================allMatch使用結束==========>>>>>>>>>>> =================anyMatch使用開始==========>>>>>>>>>>> bool = true =================anyMatch使用結束==========>>>>>>>>>>> =================collect使用開始==========>>>>>>>>>>> lists.size: 3 value = Ulrica value = Cecilia value = Claudia =================collect使用結束==========>>>>>>>>>>> =================reduce使用開始==========>>>>>>>>>>> UlricaQuellaCeciliaClaudiaDesdemonaIndira =================reduce使用結束==========>>>>>>>>>>> =================統計大於2小於5使用開始==========>>>>>>>>>>> key = 2 value = 4 key = 3 value = 6 key = 4 value = 12 key = 5 value = 15 key = 6 value = 12 =================統計大於2小於5使用結束==========>>>>>>>>>>>