stream流式javajdk8引进过来的一个重要的特性,用来处理数据的集合。
它提供了一种流式处理数据的方式,可以进行过滤、映射、排序、归约等操作。以下是一些常用的Stream API:
-
filter(Predicate):通过给定的条件(Predicate),过滤流中的元素,返回满足条件的元素流。
-
map(Function):对流中的每个元素,通过给定的转换函数(Function)进行转换,返回转换后的元素流。
-
flatMap(Function):对流中的每个元素,通过给定的转换函数(Function)进行转换,并将结果流扁平化为一个流。
-
sorted():对流中的元素进行默认的自然排序。
-
sorted(Comparator):对流中的元素根据给定的比较器(Comparator)进行排序。
-
distinct():去除流中的重复元素,返回去重后的元素流。
-
limit(long):从流中截取指定数量的元素,返回截取后的元素流。
-
skip(long):跳过指定数量的元素,返回剩余的元素流。
-
forEach(Consumer):对流中的每个元素执行给定的操作(Consumer)。
-
collect(Collector):将流中的元素收集到一个容器中,如List、Set、Map等。
-
reduce(BinaryOperator):使用指定的二元操作(BinaryOperator),对流中的元素进行归约操作,返回一个Optional。
-
anyMatch(Predicate):判断是否至少有一个元素满足给定的条件(Predicate)。
-
allMatch(Predicate):判断是否所有元素都满足给定的条件(Predicate)。
-
noneMatch(Predicate):判断是否所有元素都不满足给定的条件(Predicate)。
下面是代码
stream的创建
public static void main(String[] args) {
//stream的创建
//list
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张三更","张三丰","张大爷","张但","鹭湖宫萨达");
Stream<String> stream1 = list.stream();
//set
Set<String> set = new HashSet<>();
Collections.addAll(set,"张三更","张三丰","张大爷","张但","鹭湖宫萨达");
Stream<String> stream2 = set.stream();
//map
Map<String, Integer> map = new HashMap<>();
//使用keySet
Set<String> keySet = map.keySet();
Stream<String> stream3 = keySet.stream();
//使用values
Collection<Integer> values = map.values();
Stream<Integer> stream4 = values.stream();
//使用entrySet
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Stream<Map.Entry<String, Integer>> stream5 = entries.stream();
//数组
String [] arr={"张三更","张三丰","张大爷","张但","鹭湖宫萨达"};
Stream<String> stream6 = Arrays.stream(arr);
Stream<String> stream7 = Stream.of(arr);
}
stream流的中间件
public static void main(String[] args) {
//stream流常见的中间方法
ArrayList<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 12, 53);
Student s2 = new Student("王五", 34, 32);
Student s3 = new Student("刘明", 22, 99);
Student s4 = new Student("李明", 23, 666);
Student s5 = new Student("盖亚", 12, 999);
Collections.addAll(list,s1,s2,s3,s4,s5);
//需求:找出成就大于等于60的数据,升序后,在打印
list.stream()
//过滤
.filter(student -> student.getScore()>=60)
//排序
.sorted(Comparator.comparingDouble(Student::getScore))
//遍历集合打印元素对象
.forEach(student -> System.out.println(student));
System.out.println("-----------------------------");
//需求:找出年龄大于等于23,且年龄小于等于30岁的学生,并且按照年龄排序输出
list.stream()
//过滤
.filter(student -> student.getAge()>=23&&student.getAge()<=30)
//排序
.sorted((o1, o2) -> o2.getAge()-o1.getAge())
.forEach(System.out::println);
System.out.println("-----------------------------");
//需求3:取出成绩最高,的前三名学生,并输入.
list.stream()
.sorted((o1, o2) -> Double.compare(o2.getScore(),o1.getScore()))
//获取前三的元素
.limit(3)
//方法声明引用
.forEach(System.out::println);
System.out.println("-----------------------------");
//
list.stream()
.sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o1.getScore(),o2.getScore());
}
})
//获取最后的元素
.skip(list.size()-2)
.forEach(System.out::println);
System.out.println("-----------------------------");
//需求5:找出分数超出50的学生叫什么名字,要求去除重复的名字,在输出
list.stream().filter(student -> student.getScore()>50)
//
.map(student -> student.getName())
//去重
.distinct()
.forEach(System.out::println);
System.out.println("-----------------------------");
//使用concat连接两个stream流
Stream<String> stream1 = Stream.of("张三", "王五");
Stream<String> stream2 = Stream.of("小黑", "小白");
Stream<String> stream = Stream.concat(stream1, stream2);
stream.forEach(System.out::println);
System.out.println("-----------------------------");
}
stream流的终极方法
public static void main(String[] args) {
//Stream的常见终极方法
ArrayList<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 12, 53);
Student s2 = new Student("王五", 34, 32);
Student s3 = new Student("刘明", 22, 99);
Student s4 = new Student("李明", 23, 666);
Student s5 = new Student("盖亚", 12, 999);
Collections.addAll(list,s1,s2,s3,s4,s5);
//需求1: 分数大于50的学生,有几个人
long count = list.stream().filter(student -> student.getScore() > 50).count();
System.out.println(count);
System.out.println("-----------------------");
//需求2 : 找出分数最大的学生,打印输出
Optional<Student> optional = list.stream().max((o1, o2) -> Double.compare(o1.getScore(), o2.getScore()));
//get()获得optional其中的元素
Student student = optional.get();
System.out.println(student);
//optional.ifPresent 如果这个optional中的元素不为空,则---
optional.ifPresent(System.out::println);
System.out.println("-----------------------");
//需求3:找出分数最小的学生,打印输出
Student student1 = list.stream().min(((o1, o2) -> Double.compare(o1.getScore(), o2.getScore()))).get();
System.out.println(student1);
System.out.println("-----------------------");
//需求4 找出分数超过80分的学生,放在新的集合中返回
List<Student> collect = list.stream()
//过滤
.filter(student2 -> student2.getScore() > 80)
//创建新的集合,list集合
.collect(Collectors.toList());
System.out.println(collect);
Set<Student> collect1 = list.stream()
.filter(student2 -> student2.getScore() > 80)
//创建新的集合,set集合
.collect(Collectors.toSet());
System.out.println(collect1);
//需求5 出分数超过80分的学生,放在新的集合中返回
// key为学生姓名 value为学生成绩 map集合
Map<String, Double> collect2 = list.stream()
.filter(student2 -> student2.getScore() >= 80)
//collect,集合转换 当前list转Map
.collect(Collectors.toMap(k -> k.getName(), v -> v.getScore()));
System.out.println(collect2);
}