文章目录
一、stream流的使用流程
1.创建流:有且创建一次即可。
2. 中间操作:0个,1个及多个均可,可以进行链式操作。
3. 终端操作:一条语句中有且只存在1个,一旦进行该操作,代表该流已结束。
我们需要关注的,是对流的中间操作和终端操作。
二、创建流
串行流线程安全,保证顺序;并行流线程不安全,不保证顺序,但是快。
// 串行流
Stream<User> stream = users.stream();
// 并行流
Stream<User> stream = users.parallelStream();
三、实际应用
例子:创建一个Student实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id;
private String name;
private String address;
private Integer age;
private String flag;
}
业务场景:有一个List列表,我们需要获得年龄为70岁的前2个Student的姓名。
- 传统的实现方式:
List<Student> list = new ArrayList<>();
list.add(new Student(1l, "刘亦菲", "北京市", 70, "Y"));
list.add(new Student(2l, "张明明", "北京市", 70, "Y"));
list.add(new Student(3l, "李萌萌", "北京市", 70, "Y "));
list.add(new Student(4l, "赵侃侃", "北京市", 56, "Y"));
List<String> nameList = new ArrayList(); // 收集的姓名集合
for(Student student: list){
if(student.getAge() == 70){ // 满足条件
nameList.add(student.getName); // 加入姓名集合
if(nameList.size() >= 10){ // 判断是否超过限制
break;
}
}
}
- 通过stream实现:
List<Student> list = new ArrayList<>();
list.add(new Student(1l, "刘亦菲", "北京市", 70, "Y"));
list.add(new Student(2l, "张明明", "北京市", 70, "Y"));
list.add(new Student(3l, "李萌萌", "北京市", 70, "Y "));
list.add(new Student(4l, "赵侃侃", "北京市", 56, "Y"));
List<String> nameList = list.stream().filter(student -> student.getAge() == 70) // 过滤条件
.limit(2) // limit限制条件
.map(student -> student.getName()) // 获得姓名
.collect(Collectors.toList()); // 转化为list
nameList.forEach(student -> System.out.println(student));
四、stream常用方法
javaBean准备:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id;
private String name;
private String address;
private Integer age;
private String flag;
}
1、forEach()迭代数据
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
// java 8 stream lambda
list.stream().forEach(student -> System.out.println(student));
打印结果:
2、sorted() 排序数据(默认顺序排序,reverse方法倒序排序)
- 按照年龄顺序排序
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
list.stream().sorted(Comparator.comparing(Student::getAge)).forEach(student -> System.out.println(student));
打印结果:
- 按照年龄降序排序
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(student -> System.out.println(student));
打印结果:
3、filter():过滤
业务:过滤出年龄大于45的人
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
list.stream().filter((student -> student.getAge() > 45)).forEach(student -> System.out.println(student));
打印结果:
4、limit():使用该方法截断
业务:只输出前两个
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
list.stream().limit(2).forEach(student -> System.out.println(student));
打印结果:
5、skip():与limit互斥,使用该方法跳过元素
业务:跳过前2个元素,从第3个开始输出
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
list.stream().skip(2).forEach(student -> System.out.println(student));
打印结果:
6、max,min,sum,avg,count
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
IntSummaryStatistics num = list.stream().mapToInt(student -> student.getAge())
.summaryStatistics();
System.out.println("总共人数:" + num.getCount());
System.out.println("平均年龄:" + num.getAverage());
System.out.println("最大年龄:" + num.getMax());
System.out.println("最小年龄:" + num.getMin());
System.out.println("年龄之和:" + num.getSum());
打印结果:
7、map():接收一个方法作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
List<Integer> ages = list.stream().map(user -> user.getAge()).collect(Collectors.toList());
ages.forEach(age -> System.out.println(age));
打印结果:
8、findFirst() :使用该方法获取第一个元素
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
Student student = list.stream().findFirst().get();
System.out.println(student);
9、reduce() :多方面操作
reduce 操作可以实现从一组元素中生成一个值
sum()、max()、min()、count()等都是reduce操作。
业务:找到年龄最大的
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
Optional<Student> reduce = list.stream().reduce((s1, s2) -> s1.getAge() > s2.getAge() ? s1 : s2);
Student student = reduce.get();
System.out.println(student);
10、anyMatch / allMatch / noneMatch
anyMatch:判断的条件里,任意一个元素成功,返回true
allMatch:判断条件里的元素,所有的都是,返回true
noneMatch:与allMatch相反,判断条件里的元素,所有的都不是,返回true
业务:判断集合中的flag是否都是Y
List<Student> list = new ArrayList<>();
list.add(new Student(1l,"刘亦菲","北京市",28,"Y"));
list.add(new Student(2l,"张明明","北京市",34,"Y"));
list.add(new Student(3l,"李萌萌","北京市",12,"Y"));
list.add(new Student(4l,"赵侃侃","北京市",46,"Y"));
boolean boo = list.stream().allMatch(student -> "Y".equals(student.getFlag()));
System.out.println(boo);
打印结果:
资源连接:stream的相关API操作