-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStream.java
More file actions
70 lines (58 loc) · 2.08 KB
/
Copy pathTestStream.java
File metadata and controls
70 lines (58 loc) · 2.08 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright 2021 tu.cn All right reserved. This software is the
* confidential and proprietary information of tu.cn ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Tu.cn
*/
package com.ariix.java8.stream;
import org.junit.Before;
import org.junit.Test;
import sun.java2d.SurfaceDataProxy;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Streams
*
* 1. 创建数据源
**/
public class TestStream {
private static final Integer[] integers = new Integer[10];
Random random =new Random();
@Before
public void setup() {
for (int i = 0; i < integers.length; i++) {
integers[i] = random.nextInt(100);
}
}
@Test
public void testDatasource( ) {
//通过collections集合提供的Stream 或者paralelStream
List<String> list = new ArrayList<>();
Stream<String> stringStream = list.stream();
Stream<String> parallelStream = list.parallelStream();
//通过Arrays中的静态方法stream()获取数组流
Stream<Integer> stream = Arrays.stream(integers);
//3通过stream类中的静态方法
Stream<String> stream1 = Stream.of("jell" , "li");
//4 创建无限流
Stream<Integer> stream4 = Stream.iterate(1, x -> x + 5);
// stream4.forEach(System.out::println);
//5 创建无限流
Random random = new Random();
Stream<Integer> stream5 = Stream.generate(()-> random.nextInt(42000));
stream5.forEach(System.out::println);
}
@Test
public void testStreamMap() {
System.out.println(Arrays.toString(integers));
List<String> result = Arrays.stream(integers).filter(x-> x >50).map(x-> x + " good").collect(Collectors.toList());
System.out.println(Arrays.asList(result));
}
}