jdk8出來日子不短了,jdk11都出來了,不過用的最多的不過是1.5罷了。php
今年終於鼓起勇氣認真對待它,在18年記錄下學習stream,畫上一個圓。html
先看個圖java
Java8中有兩大最爲重要的改變。第一個是Lambda 表達式;另一個則是Stream API(java.util.stream.*)。sql
說說stream吧。前提得有lambda的基礎。數據庫
Stream 是Java8 中處理集合的關鍵抽象概念,它能夠指定你但願對集合進行的操做,能夠執行很是複雜的查找、過濾和映射數據等操做。使用Stream API 對集合數據進行操做,就相似於使用SQL 執行的數據庫查詢。也可使用Stream API 來並行執行操做。簡而言之,Stream API 提供了一種高效且易於使用的處理數據的方式。編程
對stream的操做分爲三類。數組
流程以下圖數據結構
雖然大部分狀況下stream是容器調用Collection.stream()方法獲得的,但stream和collections有如下不一樣:ide
@Test public void test5() { List<String> collect = list.stream() .filter(p -> p.getId() > 1) .sorted(Comparator.comparing(Star::getScore)) .map(Star::getName) .collect((Collectors.toList())); System.out.println(collect); }
就是這麼的容易。函數式編程
package com.test; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import org.junit.Test; public class DoubleColonTest { public static void myPrint(String str) { System.out.println("print value : " + str); } /** * 不使用雙冒號 */ @Test public void test1() { List<String> list = Arrays.asList("劉德華","黎明","張學友","郭富城"); list.forEach(p -> myPrint(p)); } /** * 使用雙冒號 */ @Test public void test2() { List<String> list = Arrays.asList("劉德華","黎明","張學友","郭富城"); list.forEach(DoubleColonTest::myPrint); } /** * 相似於雙冒號 */ @Test public void test3() { List<String> list = Arrays.asList("劉德華","黎明","張學友","郭富城"); Consumer<String> methodParam = DoubleColonTest::myPrint; list.forEach(methodParam); } /** * 相似於雙冒號 */ @Test public void test4() { List<String> list = Arrays.asList("劉德華","黎明","張學友","郭富城"); Consumer<String> methodParam = DoubleColonTest::myPrint; list.forEach(p -> methodParam.accept(p)); } }
完整代碼實例
package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.Before; import org.junit.Test; public class TestStream { List<Star> list = null; @Before public void before() { list = new ArrayList<Star>() { { add(new Star(1, "張學友", 11.3)); add(new Star(3, "劉德華", 4.3)); add(new Star(2, "黎明", 13.3)); add(new Star(5, "郭富城", 22.3)); add(new Star(4, "范冰冰", 2.3)); } }; } /** * 遍歷方式 */ @Test public void test1() { System.out.println("第一種---------"); for (Star Star : list) { System.out.println(Star); } System.out.println("第二種---------"); list.forEach(p -> System.out.println(p)); System.out.println("第三種---------"); list.forEach(System.out::println); System.out.println("第四種---------"); Iterator<Star> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("第五種---------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("第六種---------"); for (Iterator<Star> it = list.iterator(); it.hasNext();) { System.out.println(it.next()); } System.out.println("第七種---------"); for (int i = 0; i < list.size();) { System.out.println(list.get(i)); i++; } } /** * 普通排序 */ @Test public void test2() { Collections.sort(list, new Comparator<Star>() { @Override public int compare(Star o1, Star o2) { return o1.getScore().compareTo(o2.getScore()); } }); list.forEach(p -> System.out.println(p)); } /** * lambda排序 */ @Test public void test3() { Collections.sort(list, (p1, p2) -> p1.getScore().compareTo(p2.getScore())); list.forEach(p -> System.out.println(p)); } /** * streame排序 */ @Test public void test4() { Stream<Star> stream = list.stream().sorted(Comparator.comparing(Star::getScore));// .forEach(p ->
stream.forEach(p -> System.out.println(p)); // list.forEach(p -> System.out.println(p));
} /** * 進行過濾操做 */ @Test public void test5() { List<String> collect = list.stream() .filter(p -> p.getId() > 1) .sorted(Comparator.comparing(p -> p.getScore())) // .sorted(Comparator.comparing(Star::getScore))
.map(Star::getName) .collect((Collectors.toList())); System.out.println(collect); } } class Star { private Integer id; private String name; private Double score; public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Star() { super(); } public Star(Integer id, String name, Double score) { super(); this.id = id; this.name = name; this.score = score; } @Override public String toString() { return "Star [id=" + id + ", name=" + name + ", score=" + score + "]"; } }
OK,到位,入個門。。。