首先我们来看看什么是Stream!
那么我们可以看出,它其实是为了操作我们的数据,接下来就来看看具体操作吧。
@Test
/**
* 一、Stream的三个操作步骤
* 1.创建Stream
* 2.中间操作
* 3.终止操作(终端操作)
*
*/
public void testStream(){
//生成流的几种方式
//注释:一个stream流只能使用一次
//1.可以通过Collection系列集合提供的stream()或parallelStream()
List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
//2.通过Arrays中的静态方法stream()获取数组流
Integer[] integers = new Integer[10];
Stream<Integer> integerStream = Arrays.stream(integers);
//3.通过Stream类中的静态方法of()
Stream<String> stringStream = Stream.of("1", "3", "5");
//4.创建无限流
//迭代方式
Stream.iterate(0, (x) -> x + 2)
//中间操作
.limit(10)
//终止操作
.forEach(System.out::println);
//生成方式
Stream.generate(()->Math.random())
.limit(5)
.forEach(System.out::println);
//-----------------------------------
//中间操作
/**
* 筛选与切片
* filter 接收Lambda,从流中排除某些元素
* limit 截断流,使其元素不超过给定数量
* skip(n) 跳过元素,返回一个丢掉了前n个元素的流。若流中元素不足b个,则返回一个空流,与limit(n)互补
* distinct 筛选,通过流所生成元素的hashCode()和equals() 去除重复元素
*
*/
List<Student> studentList = Arrays.asList(new Student().setName("张三").setStature(1.67).setScore(100).setStatus(Student.Status.FREE),
new Student().setName("李四").setStature(1.77).setScore(104).setStatus(Student.Status.BUSY),
new Student().setName("王五").setStature(1.87).setScore(78).setStatus(Student.Status.FREE),
new Student().setName("赵六").setStature(1.87).setScore(88).setStatus(Student.Status.VOCATION),
new Student().setName("李四").setStature(1.77).setScore(104).setStatus(Student.Status.BUSY),
new Student().setName("李四").setStature(1.77).setScore(104).setStatus(Student.Status.BUSY));
//中间操作:不会执行任何操作
Stream<Student> studentStream = studentList.stream()
.filter(student -> student.getStature() > 1.75);
//终止操作:一次性执行全部内容,即“惰性求值”
//studentStream.forEach(System.out::println);
studentList.stream()
.filter(student -> student.getScore()>80)
.skip(2)
.distinct()//需要先重写ashCode()和equals()
.forEach(System.out::println);
/**
* 映射
* map 接收一个Lambda,将元素转换成其它形式或提取信息。接收一个函数
* 作为参数,该函数会被应用到每个元素上,并将其映射成一个新元素
* flatMap 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有
* 流连成一个流
*/
Arrays.asList("aaa","bbb","ccc","ddd","eee").stream()
.map(s -> s.toUpperCase())
.forEach(System.out::println);
studentList.stream()
.map(Student::getName)
.forEach(System.out::println);
/**
* 排序
* sorted() 自然排序
* sorted(Comparator com) 定制排序
*/
studentList.stream()
.sorted((student1,student2)->student2.getScore()-student1.getScore())
.forEach(System.out::println);
/**
* 查找与匹配
* allMatch 检查是否匹配所有元素
* anyMatch 检查是否至少匹配一个元素
* noneMatch 检查是否没有匹配所有元素
* findFirst 返回第一个元素
* findAny 返回当前流中的任意元素
* count 返回流中元素的总个数
* max 返回流中最大值
* min 返回流中最小值
*
*/
//是否所有元素都满足
boolean allMatch = studentList.stream()
.allMatch(student -> student.getStatus().equals(Student.Status.BUSY));
System.out.println(allMatch);
//是否有元素满足
boolean anyMatch = studentList.stream()
.anyMatch(student -> student.getStatus().equals(Student.Status.BUSY));
System.out.println(anyMatch);
//返回第一个元素
Optional<Student> optionalStudent = studentList.stream()
.sorted((student1, student2) -> student2.getScore() - student1.getScore())
.findFirst();
System.out.println(optionalStudent.get());
//随便找出一个空闲的学生
//并行流,从上到下找一个空闲的,可以改成parallelStream串行流,随机找一个空闲的
studentList.stream()
.filter(student -> student.getStatus().equals(Student.Status.FREE))
.findAny();
long count = studentList.stream()
.count();
System.out.println(count);
Optional<Student> max = studentList.stream()
.max((s1, s2) -> Integer.compare(s1.getScore(), s2.getScore()));
System.out.println(max.get());
Optional<Integer> min = studentList.stream()
.map(Student::getScore)
.min(Integer::compare);
System.out.println(min.get());
/**
* 归约
* reduce(T identity,BinaryOperator)/reduce(BinaryOperator)
* 可以将流中的元素反复结合起来,得到一个值
*/
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer reduce = integerList.stream()
.reduce(0, (x, y) -> x + y);
System.out.println(reduce);
/**
* 收集
* collect 将流转换成其它形式,接收一个Collector接口的实现,用于给Stream中元素做汇总的方法
*
*/
List<String> collectNmae = studentList.stream()
.map(Student::getName)
.collect(Collectors.toList());
HashSet<String> hashSet = studentList.stream()
.map(Student::getName)
.collect(Collectors.toCollection(HashSet::new));
//平均值
Double aveStature = studentList.stream()
.collect(Collectors.averagingDouble(Student::getStature));
//分组
Map<Student.Status, List<Student>> statusListMap = studentList.stream()
.collect(Collectors.groupingBy(Student::getStatus));
//分区,true一个区,false一个区
Map<Boolean, List<Student>> booleanListMap = studentList.stream()
.collect(Collectors.partitioningBy(e -> e.getScore() > 95));
}
当然,stream流的操作还有很多,这里就不一一列举了,有兴趣的工程师可以区看看stream api相信如果用起来可以让你代码变得更加简洁方便。