首先要說的是,不要被它的名稱騙了,這裏的Stream跟JAVA I/O中的InputStream和OutputStream是兩個不一樣的概念。Java 8中的Stream實際上是函數式編程裏Monad的概念,關於Monad,感受仍是比較抽象,很差理解,能夠參考這篇文章,我的以爲仍是比較好看懂的,簡單說,Monad就是一種設計模式,表示將一個運算過程,經過函數拆解成互相鏈接的多個步驟,有點鏈式操做的感受。html
以下,是一個Stream的例子java
import java.util.Arrays; import java.util.List; public class Snippet { public static void main(String[] args) { List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1"); myList .stream() .filter(s -> s.startsWith("c")) //過濾以c字母開頭 .map(String::toUpperCase) //字符變成大寫 .sorted() //排序 .forEach(System.out::println); //打印輸出 } }
Stream有兩種類型的操做:Intermediate操做和Terminal操做。編程
Stream能夠進行屢次的Intermediate操做,如前面開頭的那個例子,其中filter、map、sorted都是Intermediate操做,注意該操做是惰性化的,當調用到該方法的時候,並無真正開始Stream的遍歷。設計模式
一個Stream只有一個Terminal操做,如前面開頭的那個例子,其中forEach就是Terminal操做,Terminal操做是Stream的最後一個操做,這時候纔會開始Stream的遍歷。api
使用Stream.of數組
import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9); stream.forEach(p -> System.out.println(p)); } }
使用Arrays.stream數據結構
import java.util.Arrays; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9}); stream.forEach(p -> System.out.println(p)); } }
使用Collection.stream() or Collection.parallelStream()多線程
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 })); Stream<Integer> stream = list.stream(); //or list.parallelStream(); stream.forEach(p -> System.out.println(p)); } }
使用IntStream.range併發
import java.util.stream.IntStream; public class StreamBuilders { public static void main(String[] args) { IntStream stream = IntStream.range(1, 9); stream.forEach(p -> System.out.println(p)); } }
使用Random.ints()框架
import java.util.Random; import java.util.stream.IntStream; public class StreamBuilders { public static void main(String[] args) { IntStream stream = new Random().ints(1, 10); stream.forEach(p -> System.out.println(p)); } }
使用Stream.generate()
import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public class StreamBuilders { static int i = 0; public static void main(String[] args) { Stream<Integer> stream = Stream.generate(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } return i++; }); stream.forEach(p -> System.out.println(p)); } }
其它還有不少,這裏暫不一一列出。
使用stream.collect(Collectors.toList())
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 })); Stream<Integer> stream = list.stream(); List<Integer> evenNumbersList = stream.filter(i -> i % 2 == 0).collect(Collectors.toList()); System.out.print(evenNumbersList); } }
使用stream.toArray(EntryType[]::new)
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 })); Stream<Integer> stream = list.stream(); Integer[] evenNumbersArr = stream.filter(i -> i % 2 == 0).toArray(Integer[]::new); System.out.print(Arrays.asList(evenNumbersArr)); } }
其它轉爲set,map的相似,不一一列出。
Intermediate(中間操做),這裏只列出常見的幾個
filter方法,過濾元素
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList(new String[]{ "Amitabh", "Shekhar", "Aman", "Rahul", "Shahrukh", "Salman", "Yana", "Lokesh"})); Stream<String> stream = list.stream(); stream.filter((s) -> s.startsWith("A")).forEach(System.out::println); } }
map方法,修改元素
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList(new String[]{ "Amitabh", "Shekhar", "Aman", "Rahul", "Shahrukh", "Salman", "Yana", "Lokesh"})); Stream<String> stream = list.stream(); stream.filter((s) -> s.startsWith("A")).map(String::toUpperCase).forEach(System.out::println); } }
sorted方法,排序,能夠傳入自定義排序接口Comparator,
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList(new String[]{ "Amitabh", "Shekhar", "Aman", "Rahul", "Shahrukh", "Salman", "Yana", "Lokesh"})); Stream<String> stream = list.stream(); stream.sorted().map(String::toUpperCase).forEach(System.out::println); } }
Terminal(結束操做),這裏只列出常見的幾個
這裏的例與前面的相似,就不寫出所有代碼了,列出重要部分。
forEach方法,迭代元素,並執行相關操做
stream.sorted().map(String::toUpperCase).forEach(System.out::println);
collect方法,從Stream中獲得集合
List<String> memNamesInUppercase = stream.sorted().map(String::toUpperCase).collect(Collectors.toList());
System.out.print(memNamesInUppercase);
Match方法,匹配判斷Stream中的元素是否符合指定規則
boolean matchedResult = list.stream().anyMatch((s) -> s.startsWith("A")); System.out.println(matchedResult); matchedResult = list.stream().allMatch((s) -> s.startsWith("A")); System.out.println(matchedResult); matchedResult = list.stream().noneMatch((s) -> s.startsWith("A")); System.out.println(matchedResult);
count方法,計數
long totalMatched = list.stream().filter((s) -> s.startsWith("A")).count(); System.out.println(totalMatched);
reduce方法,元素組合操做,經常使用於字符串拼接、數值的 sum、min、max、average
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; public class StreamBuilders { public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList(new String[]{ "Amitabh", "Shekhar", "Aman", "Rahul", "Shahrukh", "Salman", "Yana", "Lokesh"})); Optional<String> reduced = list.stream().reduce((s1, s2) -> s1 + "#" + s2); reduced.ifPresent(System.out::println); //打印結果:Amitabh#Shekhar#Aman#Rahul#Shahrukh#Salman#Yana#Lokesh } }
所謂的短路操做。指的是若是符合要求的話,就不繼續執行接下來的操做,相似於&&和||操做,
在Stream中,相似的有anyMatch()和findFirst()方法,
anyMatch(),返回布爾值,只要找到一個匹配的元素,就中止接下來的元素遍歷;
boolean matched = list.stream().anyMatch((s) -> s.startsWith("A")); System.out.println(matched); // Output: true
findFirst(),返回元素,一樣,只返回第一個元素,不會所有遍歷;
String firstMatchedName = list.stream().filter((s) -> s.startsWith("L")).findFirst().get(); System.out.println(firstMatchedName); // Output: Lokesh
Java 7引入了Fork/Join並行計算框架,能讓咱們以並行方式來拆分任務和加速處理過程。一般編寫並行代碼很難並且容易出錯, 但使用 Stream API 無需編寫一行多線程的代碼,就能夠很方便地寫出高性能的併發程序。
以下示例:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class StreamBuilders { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(Arrays.asList(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 })); // Here creating a parallel stream Stream<Integer> stream = list.parallelStream(); Integer[] evenNumbersArr = stream.filter(i -> i % 2 == 0).toArray(Integer[]::new); System.out.print(Arrays.asList(evenNumbersArr)); } }
下面給出一個使用Stream與不使用Stream示例,用於統計字符長度爲3的字符串個數。
import java.util.Arrays; import java.util.List; public class Java8Tester { public static void main(String args[]){ List<String> strings = Arrays.asList("abc", "111", "bc", "efg", "12584","", "1254"); //使用Java 7, 統計字符長度爲3的字符串個數 long count = 0; for(String string: strings){ if(string.length() == 3){ count++; } } System.out.println("using java7:Strings of length 3: " + count); //使用Java 8的stream, 統計字符長度爲3的字符串個數 count = strings.stream().filter(string -> string.length() == 3).count(); System.out.println("using java8:Strings of length 3: " + count); } }
http://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/
http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
https://www.tutorialspoint.com/java8/java8_streams.htm
http://howtodoinjava.com/core-java/collections/java-8-tutorial-streams-by-examples/