First, examples
Importjava.util.Arrays;Importjava.util.List;Importjava.util.stream.Collectors;ImportJava.util.stream.Stream;/*** Created by Root on 16-5-19.*/ Public classStreamtest {/*** Set of not less than 50 of the number output out [1, ,,,,, 185, Max.*/ Private Static voidfun1 () {int[] A = {1, 20, 63, 58, 185, 60, 59, 20}; Arrays.stream (a). Filter (x(x >=50) . ForEach (system.out::p rintln); } /*** Use Stream's own properties to determine if the following collections are all larger than 50,[1, (+, +, +-185, +/-)*/ Private Static Booleanfun2 () {int[] A = {1, 20, 63, 58, 185, 60, 59, 20}; returnArrays.stream (a). Allmatch (P, p > 50); } /*** Output The same string that exists in the following two collections, ["Qwer", "asdf", "ZXCV", "FGH", "Jum", "RFV"],["EDC", "Zse", "asdf", "ZXCV", "Yhnj"]*/ Private Static voidFun3 () {string[] S1= {"Qwer", "asdf", "ZXCV", "FGH", "Jum", "RFV"}; string[] S2= {"EDC", "Zse", "asdf", "ZXCV", "Yhnj"}; Arrays.stream (S1). ForEach (x-{arrays.stream (S2). Filter (Y-y.equals (x)). ForEach (System.out::p rintln); }); } /*** Multiply all the numbers in the following set by 10, and get a new set [1, +,, +, 185,*/ Private Static voidFun4 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Map (n-n *). ToArray (integer[]::New); Arrays.stream (num). ForEach (System.out::p rintln); } /*** Remove the 3rd to 7th value from the following set, plus 5, to get a new set [1, ten, seven, seven, 185, X, Min, +]*/ Private Static voidfun5 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Skip (2). Limit (5). Map (x, x+5). ToArray (integer[]::New); Arrays.stream (num). ForEach (System.out::p rintln); } /*** Output The minimum value of the following set [1, 185, Min .,.*/ Private Static voidfun6 () {int[] Nums = {1, 20, 63, 58, 185, 60, 59, 20}; System.out.println (Arrays.stream (nums). Min (). Getasint ()); } /*** Sort from small to large for the following collections [1, +, +, 185, +,--]*/ Private Static voidfun7 () {int[] Nums = {1, 20, 63, 58, 185, 60, 59, 20}; Arrays.stream (nums). Sorted (). ForEach (System.out::p rintln); } /*** Collection Merge: ["Qwe", "DFG", "SSS"],["Qwe", "DFG", "SSS"],["Qwe", "DFG", "SSS"] * merged into ["Qwe", "DFG", "SSS", "Qwe", "DFG", "SSS", "Qwe", "DFG", "SSS"]*/ Private Static voidFun8 () {Stream<List<String>> InputStream =Stream.of (Arrays.aslist ("Qwe", "DFG", "SSS"), Arrays.aslist ("Rty", "ghkj", "Fghh"), Arrays.aslist ("GTB", "SSD", "DG") ); Stream<String> outputstream = Inputstream.flatmap (P-P.stream ()); List<String> str =Outputstream.collect (Collectors.tolist ()); System.out.println (str); } /*** Take out the following set of more than 50 and get a new set [1, +, +,, 185, +,--]*/ Private Static voidFun9 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Filter (P > P). ToArray (integer[]::New); Arrays.stream (num). ForEach (System.out::p rintln); } /*** Convert the following names to uppercase, then sort the output ("Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell")*/ Private Static voidFun10 () {string[] str= {"Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell"}; Arrays.stream (str). Map (P-p.touppercase ()). Sorted (). ForEach (System.out::p rintln); } Public Static voidMain (string[] args) {fun8 (); }}
Stream of Java (JDK8)