The Ways to create Stream in Java 8

摘要:本文介绍Java8 中所有可以生成 Stream 的方法。

通过 Collection 类直接生成

1
2
3
4
5
6
// from collection
List<String> list = new ArrayList<String>();
list.add("java");
list.add("php");
list.add("python");
stream = list.stream();

通过 Array 直接生成

1
2
3
4
5
6
7
8
9
String[] arr = { "program", "creek", "program", "creek", "java", "web",
"program" };
stream = Stream.of(arr);

stream = Stream.of("program", "creek", "program", "creek", "java",
"web", "program");

String[] stringArr = {"a", "b", "c", "d"};
Stream<String> stream = Arrays.stream(stringArr);

通过 Stream 的 of 静态方法

1
Stream<String> stream = Stream.of("hello", "world", "helloworld");

通过 Stream 的 generate 静态方法

1
2
3
4
5
// generate()
Stream<String> stream = Stream.generate(() -> "test").limit(10);

String[] strArr = stream.toArray(String[]::new);
System.out.println(Arrays.toString(strArr))

通过 Stream 的 iterate 静态方法

1
2
3
4
5
// iterate()
Stream<BigInteger> bigIntStream =
Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE)).limit(10);
BigInteger[] bigIntArr = bigIntStream.toArray(BigInteger[]::new);
System.out.println(Arrays.toString(bigIntArr));

通过 java.util.Spliterator 和 StreamSupport 生成

这种方法对于将 iterable 转换为 stream 十分有用,比如我们某个 repository 的接口有一个方法 findAll 返回一个 Iterable,这时候我们就可以用这种方法获取 stream 并进行操作:

1
2
3
4
return StreamSupport.stream(unitOfMeasureRepository.findAll()
.spliterator(), false)
.map(unitOfMeasureToUnitOfMeasureCommand::convert)
.collect(Collectors.toSet());

其他案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
ArrayList<Integer> number_List = new ArrayList<>(); 
Spliterator<Integer> mySpliterator, myParallelSpliterator;

number_List.add(1);
number_List.add(2);
number_List.add(3);
number_List.add(4);
number_List.add(5);

mySpliterator = number_List.spliterator();
myParallelSpliterator = number_List.spliterator();

System.out.println("Characteristics Of Spliterator are : "+mySpliterator.characteristics());
System.out.println("estimateSize Of Spliterator is : "+mySpliterator.estimateSize());

Stream<Integer> intStream = StreamSupport.stream(mySpliterator.trySplit(), false);
Stream<Integer> intStream2 = StreamSupport.stream(mySpliterator.trySplit(), false);
Stream<Integer> intStream3 = StreamSupport.stream(mySpliterator.trySplit(), false);
//Stream<Integer> intStream4 = StreamSupport.stream(mySpliterator.trySplit(), false);
Stream<Integer> intStream4 = StreamSupport.stream(myParallelSpliterator.trySplit(), true);
System.out.println("All elements");
intStream
.filter((p) -> (p < 9))
.forEach(System.out::println);

System.out.println("All elements");
intStream2
.forEach(System.out::println);

System.out.println("All elements");
intStream3
.forEach(System.out::println);

System.out.println("All elements");
intStream4
.forEach(System.out::println);

通过一些其他的 API

1
2
3
4
5
// stream method from APIs
String sentence = "Program creek is a Java site.";
Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence);
String[] wordArr = wordStream.toArray(String[]::new);
System.out.println(Arrays.toString(wordArr));