stream三点特性:
1、stream不存储数据
2、stream不改变源数据
3、stream的延迟执行特性
/**
* @author yigen shi
* @date 2019/7/1 19:35
*/
public class StreamTest {
private Random random;
private List<Student> stuList;
private String[] arr1 = {"abc", "a", "bc", "abcd"};
@Before
public void init() {
random = new Random();
stuList = new ArrayList<Student>() {
{
for (int i = 0; i < 100; i++) {
add(Student.builder().name("student" + i).score(random.nextInt(50) + 50).build());
}
}
};
}
private List<User> users = Lists.newArrayList(new User(1L, "aaa", "男"),
new User(2L, "bbb", "男"),
new User(3L, "ccc", "女"),
new User(4L, "ddd", "女"));
/**
* 列出班上超过85分的学生姓名,并按照分数降序输出用户名字
*/
@Test
public void testFilter() {
List<String> studentList = stuList.stream()
.filter(x -> x.getScore() > 85)
.sorted(Comparator.comparing(Student::getScore).reversed())
.map(Student::getName)
.collect(Collectors.toList());
System.out.println(studentList);
}
/**
* 分组
*/
@Test
public void testGroupBy() {
Map<Integer, List<Student>> map = stuList.stream()
.filter(x -> x.getScore() > 85)
.collect(Collectors.groupingBy(Student::getScore));
System.out.println(map);
}
@Test
public void testGroupingBy() {
List<Student> list = new ArrayList<>();
Map<String, List<Student>> map = list.stream()
.collect(Collectors.groupingBy(Student::getName));
System.out.println(map);
}
/**
* 去重操作
*/
@Test
public void testDistinct() {
List<Integer> list = Lists.newArrayList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
List<Integer> result = list.stream().distinct().collect(Collectors.toList());
System.out.println(result);
}
/**
* 测试stream流的只能一次消费
*/
@Test(expected = Exception.class)
public void testConsume() {
Stream<String> stream = Stream.generate(() -> "user").limit(20);
stream.forEach(System.out::println);
stream.forEach(System.out::println);
}
/**
* 测试stream的延迟执行 如果stream是立即执行的话,会先打印filter方法里面的begin compare
*/
@Test
public void testDelay() {
Stream<Student> stream = stuList.stream().filter(student -> {
student.filter();
return true;
}).limit(10);
System.out.println("split-------------------------------------");
List<Student> studentList = stream.collect(Collectors.toList());
System.out.println(studentList);
}
/* ---------------创建流----------*/
/**
* 通过数组创建流
*/
@Test
public void testArrayStream() {
//1.通过Arrays.stream
//1.1基本类型
int[] arr = new int[]{1, 2, 34, 5};
IntStream intStream = Arrays.stream(arr);
long[] lrr = new long[]{1, 2, 3, 4};
LongStream longStream = Arrays.stream(lrr);
//1.2引用类型
Student[] studentArr = new Student[]{new Student("s1", 29), new Student("s2", 27)};
Stream<Student> studentStream = Arrays.stream(studentArr);
//2.通过Stream.of
Stream<Integer> stream1 = Stream.of(1, 2, 34, 5, 65);
//注意生成的是int[]的流
Stream<int[]> stream2 = Stream.of(arr, arr);
stream2.forEach(System.out::println);
}
/**
* 通过集合创建流
*/
@Test
public void testCollectionStream() {
List<String> strs = Arrays.asList("11212", "dfd", "2323", "dfhgf");
//创建普通流
Stream<String> stream = strs.stream();
//创建并行流
Stream<String> stream1 = strs.parallelStream();
}
/**
* 创建空的流
*/
@Test
public void testEmptyStream() {
//创建一个空的stream
Stream<Integer> stream = Stream.empty();
}
/**
* 创建无限流
*/
@Test
public void testUnlimitStream() {
//创建无限流,通过limit提取指定大小
Stream.generate(() -> "number" + new Random().nextI