1.建立Stream java
create Stream from Collections;dom
create Stream from values;ide
create Stream from Arrays;spa
craeate Stream from files;code
craeate Stream from functionsblog
代碼舉例:rem
1 package com.cy.java8; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 import java.util.Arrays; 8 import java.util.List; 9 import java.util.stream.Stream; 10 11 public class CreateStream { 12 13 public static void main(String[] args) { 14 createStreamFromCollection().forEach(System.out::println); 15 16 createStreamFromValues().forEach(System.out::println); 17 18 createStreamFromArrays().forEach(System.out::println); 19 20 createStreamFromFile().forEach(System.out::println); 21 } 22 23 24 /** 25 * create stream from collection 26 * 這裏list的元素的順序放到strem中不會改變,原來是什麼樣的順序,stream中就是什麼樣。 27 * @return 28 */ 29 private static Stream<String> createStreamFromCollection(){ 30 List<String> list = Arrays.asList("hello", "world", "stream"); 31 return list.stream(); 32 } 33 34 private static Stream<String> createStreamFromValues(){ 35 return Stream.of("hello", "world", "stream"); 36 } 37 38 private static Stream<String> createStreamFromArrays(){ 39 String[] strings = new String[]{"hello", "world", "stream"}; 40 return Arrays.stream(strings); 41 } 42 43 /** 44 * create stream from file 45 * @return 46 */ 47 private static Stream<String> createStreamFromFile(){ 48 Path path = Paths.get("F:\\IdeaProjects\\Java8Learning\\java8\\src\\main\\java\\com\\cy\\java8\\CreateStream.java"); 49 Stream<String> lines = null; 50 try { 51 lines = Files.lines(path); 52 } catch (IOException e) { 53 throw new RuntimeException(e); 54 } 55 return lines; 56 } 57 }
打印結果以下:get
hello world stream hello world stream hello world stream package com.cy.java8; import java.io.IOException; .... 把CreateStream.java文件分行打印出來了...
2.建立Stream string
create stream from Stream.iterate;it
create stream from Stream.generate;
代碼例子:
1 package com.cy.java8; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Getter; 5 import lombok.ToString; 6 7 import java.util.Random; 8 import java.util.function.Supplier; 9 import java.util.stream.Stream; 10 11 public class CreateStream2 { 12 13 public static void main(String[] args) { 14 createStreamFromIterator().forEach(System.out::println); 15 16 createStreamFromGenerate().forEach(System.out::println); 17 18 createObjStreamFromGenerate().forEach(System.out::println); 19 } 20 21 /** 22 * 這裏會產生一個無限的Stream 23 * 0,2,4,6,8... 24 * 能夠limit來獲取指定個數 25 * @return 26 */ 27 private static Stream<Integer> createStreamFromIterator(){ 28 Stream<Integer> stream = Stream.iterate(0, a -> a + 2).limit(3); 29 return stream; 30 } 31 32 /** 33 * 也會建立一個無限的stream 34 * 相似的IntStream,DoubleStream也是相似用法 35 * @return 36 */ 37 private static Stream<Double> createStreamFromGenerate(){ 38 Stream<Double> stream = Stream.generate(Math::random).limit(3); 39 return stream; 40 } 41 42 /** 43 * 自定義一個Obj的stream 44 * @return 45 */ 46 private static Stream<Obj> createObjStreamFromGenerate(){ 47 Stream<Obj> objStream = Stream.generate(new ObjSuppiler()).limit(3); 48 return objStream; 49 } 50 51 static class ObjSuppiler implements Supplier<Obj>{ 52 private int index = 0; 53 private Random random = new Random(System.currentTimeMillis()); 54 55 @Override 56 public Obj get() { 57 index = random.nextInt(100); 58 return new Obj(index, "Name-"+index); 59 } 60 } 61 62 @Getter 63 @AllArgsConstructor 64 @ToString 65 static class Obj{ 66 private int id; 67 private String name; 68 } 69 }
打印以下:
0 2 4 0.2300226452750267 0.48246633929281424 0.051774439637685465 CreateStream2.Obj(id=97, name=Name-97) CreateStream2.Obj(id=84, name=Name-84) CreateStream2.Obj(id=18, name=Name-18)
-----