importjava.util.stream.*;publicclassStreamOf{publicstaticvoidmain(String[] args){Stream.of(newBubble(1),newBubble(2),newBubble(3)).forEach(System.out::println);Stream.of("It's ","a ","wonderful ","day ","for ","pie!").forEach(System.out::print);System.out.println();Stream.of(3.14159,2.718,1.618).forEach(System.out::println);}}/* Output:
Bubble(1)
Bubble(2)
Bubble(3)
It's a wonderful day for pie!
3.14159
2.718
1.618
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1.1 随机数流
importjava.util.*;importjava.util.stream.*;publicclassRandomGenerators{publicstatic<T>voidshow(Stream<T> stream){
stream
.limit(4).forEach(System.out::println);System.out.println("++++++++");}publicstaticvoidmain(String[] args){Random rand =newRandom(47);show(rand.ints().boxed());show(rand.longs().boxed());show(rand.doubles().boxed());// Control the lower and upper bounds:show(rand.ints(10,20).boxed());show(rand.longs(50,100).boxed());show(rand.doubles(20,30).boxed());// Control the stream size:show(rand.ints(2).boxed());show(rand.longs(2).boxed());show(rand.doubles(2).boxed());// Control the stream size and bounds:show(rand.ints(3,3,9).boxed());show(rand.longs(3,12,22).boxed());show(rand.doubles(3,11.5,12.3).boxed());}}
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
1.2 int类型流
importstaticjava.util.stream.IntStream.*;publicclassRanges{publicstaticvoidmain(String[] args){// The traditional way:int result =0;for(int i =10; i <20; i++)
result += i;System.out.println(result);// for-in with a range:
result =0;for(int i :range(10,20).toArray())
result += i;System.out.println(result);// Use streams:System.out.println(range(10,20).sum());}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1.3 generate()
importjava.util.*;importjava.util.function.*;importjava.util.stream.*;publicclassGeneratorimplementsSupplier<String>{Random rand =newRandom(47);char[] letters ="ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();@OverridepublicStringget(){return""+ letters[rand.nextInt(letters.length)];}publicstaticvoidmain(String[] args){String word =Stream.generate(newGenerator()).limit(30).collect(Collectors.joining());System.out.println(word);}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1.4 iterate()
importjava.util.stream.*;publicclassFibonacci{int x =1;Stream<Integer>numbers(){returnStream.iterate(0, i ->{int result = x + i;
x = i;return result;});}publicstaticvoidmain(String[] args){newFibonacci().numbers().skip(20)// Don't use the first 20.limit(10)// Then take 10 of them.forEach(System.out::println);}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1.5 流的建造者模式
importjava.io.*;importjava.nio.file.*;importjava.util.stream.*;publicclassFileToWordsBuilder{Stream.Builder<String> builder =Stream.builder();publicFileToWordsBuilder(String filePath)throwsException{Files.lines(Paths.get(filePath)).skip(1)// Skip the comment line at the beginning.forEach(line ->{for(String w : line.split("[ .?,]+"))
builder.add(w);});}Stream<String>stream(){return builder.build();}publicstaticvoidmain(String[] args)throwsException{newFileToWordsBuilder("Cheese.dat").stream().limit(7).map(w -> w +" ").forEach(System.out::print);}}
importjava.io.*;importjava.nio.file.*;importjava.util.stream.*;importjava.util.regex.Pattern;publicclassFileToWordsRegexp{privateString all;publicFileToWordsRegexp(String filePath)throwsException{
all =Files.lines(Paths.get(filePath)).skip(1)// First (comment) line.collect(Collectors.joining(" "));}publicStream<String>stream(){returnPattern.compile("[ .,?]+").splitAsStream(all);}publicstaticvoidmain(String[] args)throwsException{FileToWordsRegexp fw =newFileToWordsRegexp("Cheese.dat");
fw.stream().limit(7).map(w -> w +" ").forEach(System.out::print);
fw.stream().skip(7).limit(2).map(w -> w +" ").forEach(System.out::print);}}
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
2. 流的中间操作
2.1 跟踪和调试
classPeeking{publicstaticvoidmain(String[] args)throwsException{FileToWords.stream("Cheese.dat").skip(21).limit(4).map(w -> w +" ").peek(System.out::print).map(String::toUpperCase).peek(System.out::print).map(String::toLowerCase).forEach(System.out::print);}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2.2 排序
importjava.util.*;publicclassSortedComparator{publicstaticvoidmain(String[] args)throwsException{FileToWords.stream("Cheese.dat").skip(10).limit(10).sorted(Comparator.reverseOrder()).map(w -> w +" ").forEach(System.out::print);}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2.3 移除元素
importjava.util.stream.*;importstaticjava.util.stream.LongStream.*;publicclassPrime{publicstaticbooleanisPrime(long n){returnrangeClosed(2,(long)Math.sqrt(n)).noneMatch(i -> n % i ==0);}publicLongStreamnumbers(){returniterate(2, i -> i +1).filter(Prime::isPrime);}publicstaticvoidmain(String[] args){newPrime().numbers().limit(10).forEach(n ->System.out.format("%d ", n));System.out.println();newPrime().numbers().skip(90).limit(10).forEach(n ->System.out.format("%d ", n));}}