一.常用的流
1.1Collect
将流转换为List、Set、Map等
public class Test2 {
public static void main(String[] args) {
//转换成list
List<Student> studentList = Stream.of(new Student("路飞", 22, 175),
new Student("红发", 40, 180),
new Student("白胡子", 50, 185)).collect(Collectors.toList());
System.out.println(studentList);
//转换成Map 注:存在key相同的情况需要设置策略 如下代码key相同保留最后一次value值
Map<String, Student> map = Stream.of(new Student("路飞", 22, 175), new Student("路飞", 25, 195),
new Student("红发", 40, 180),
new Student("白胡子", 50, 185)).collect(Collectors.toMap(Student::getName, Function.identity(),(oldValue,newValue)->newValue));
System.out.println(map);
}
}
打印结果:
[Student{name='路飞', age=22, stature=175, specialities=null}, Student{name='红发', age=40, stature=180, specialities=null}, Student{name='白胡子', age=50, stature=185, specialities=null}]
{白胡子=Student{name='白胡子', age=50, stature=185, specialities=null}, 红发=Student{name='红发', age=40, stature=180, specialities=null}, 路飞=Student{name='路飞', age=25, stature=195, specialities=null}}
1.2Filter
顾名思义,起过滤筛选的作用。内部就是Predicate接口。惰性求值。
//筛选出集合中对象的年龄小于等于40的
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<Student> list = students.stream().filter(scu -> scu.getAge() <= 40)
.collect(Collectors.toList());
System.out.println(list);
输出结果:
[Student{name='路飞', age=22, stature=175, specialities=null}, Student{name='红发', age=40, stature=180, specialities=null}]
1.3Map
转换功能,内部就是Function接口。惰性求值
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<String> names = students.stream().map(student -> student.getName())
.collect(Collectors.toList());
System.out.println(names);
输出结果:
[路飞, 红发, 白胡子]
1.4FlatMap
将多个Stream合并为一个Stream。惰性求值
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<Student> studentList = Stream.of(students,
asList(new Student("艾斯", 25, 183),
new Student("雷利", 48, 176)))
.flatMap(students1 -> students1.stream()).collect(Collectors.toList());
System.out.println(studentList);
输出结果:
Student{name='路飞', age=22, stature=175, specialities=null}, Student{name='红发', age=40, stature=180, specialities=null}, Student{name='白胡子', age=50, stature=185, specialities=null}, Student{name='艾斯', age=25, stature=183, specialities=null}, Student{name='雷利', age=48, stature=176, specialities=null}]
1.5Max和Min
我们经常会在集合中求最大或最小值,使用流就很方便,及早求值。
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
Optional<Student> max = students.stream()
.max(Comparator.comparing(stu -> stu.getAge()));
Optional<Student> min = students.stream()
.min(Comparator.comparing(stu -> stu.getAge()));
//判断是否有值
if (max.isPresent()) {
System.out.println(max.get());
}
if (min.isPresent()) {
System.out.println(min.get());
}
}
输出结果:
Student{name='红发', age=40, stature=180, specialities=null}
Student{name='路飞', age=22, stature=175, specialities=null}
1.6Count
统计功能,一般都是结合filter使用,因为先筛选出我们需要的再统计即可
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
long count = students.stream().filter(s1 -> s1.getAge() < 45).count();
System.out.println("年龄小于45岁的人数是:" + count);
输出结果:
年龄小于45岁的人数是:2
1.7Reduce
reduce 操作可以实现从一组值中生成一个值。在上述例子中用到的 count 、 min 和 max 方法,因为常用而被纳入标准库中。事实上,这些方法都是 reduce 操作。
Integer reduce = Stream.of(1, 2, 3, 4).reduce(0, (acc, x) -> acc+ x);
System.out.println(reduce);
输出结果:
10