展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Stream

  • 简介
Stream 中⽂称为 “流”,通过将集合转换为这么⼀种叫做 “流”的元素队列,通过声明性⽅式,能够对集合中的每个元素进⾏⼀系列并⾏或串⾏的流⽔线操作

元素是特定类型的对象,所以元素集合看作⼀种流, 流在管道中传输, 且可以在管道的节点上进⾏处理, ⽐如 排序,聚合,过滤等操作

数据元素便是原始集合,如List、Set、Map等
⽣成流,可以是串⾏流stream() 或者并⾏流 parallelStream()
中间操作,可以是 排序,聚合,过滤,转换等
终端操作,很多流操作本身就会返回⼀个流,所以多个操作可以直接连接起来,最后统⼀进⾏收集
概览stream接⼝源码
  • 案例
public class Main {

    public static void main(String[] args) throws Exception {
        List<String> list = Arrays.asList("springboot教程","微服务教程","并发编程","redis教程","消息队列教程");
        // 操作
        List<String> resultList = list.stream().map(obj->"在小滴课堂学习"+obj).collect(Collectors.toList());
        System.out.println(resultList);
    }
    
}
  • map和filter函数的使用
public class Main {

    public static void main(String[] args) throws Exception {

        // map函数
        List<User> list = Arrays.asList(new User(1,"小东","123"),new User(21,"jack","rawer"),
                new User(155,"tom","sadfsdfsdfsd"),new User(231,"marry","234324"),new User(100,"小D","122223"));
        List<UserDTO> userDTOList = list.stream().map(obj->{
            UserDTO userDTO = new UserDTO(obj.getId(),obj.getName());
            return userDTO;
        }).collect(Collectors.toList());
        System.out.println(userDTOList);

        // filter函数
        List<String> list = Arrays.asList("springboot", "springcloud", "redis", "git", "netty", "java", "html", "docker");
        List<String> resultList = list.stream().filter(obj -> obj.length() > 5).collect(Collectors.toList());
        System.out.println(resultList);
    }

}
  • limit和sorted函数使用
public class Main {

    public static void main(String[] args) throws Exception {
        List<String> list = Arrays.asList("springboot", "springcloud", "redis", "git", "netty", "java", "html", "docker");
        // 默认排序
        List<String> resultList = list.stream().sorted().collect(Collectors.toList());
        System.out.println(resultList);

        //根据长度进行排序,升序
        List<String> resultList = list.stream().sorted(Comparator.comparing(obj -> obj.length())).collect(Collectors.toList());
        // 降序
        List<String> resultList = list.stream().sorted(Comparator.comparing(obj -> obj.length(),Comparator.reverseOrder())).collect(Collectors.toList());
        List<String> resultList = list.stream().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList());

        //limit截取
        List<String> resultList = list.stream().sorted(Comparator.comparing(String::length).reversed()).limit(3).collect(Collectors.toList());
        System.out.println(resultList);
    }

}
  • allMatch和anyMatch函数的使⽤
public class Main {

    public static void main(String[] args) throws Exception {
        List<String> list = Arrays.asList("springboot", "springcloud", "redis", "git", "netty", "java", "html", "docker");
        // 全部符合条件返回true
        boolean flag1 = list.stream().allMatch(obj->obj.length()>5);
        System.out.println(flag1);
        // 只要有1个符合条件返回true
        boolean flag2 = list.stream().anyMatch(obj->obj.length()>5);
        System.out.println(flag2);
    }

}
  • max和min函数的使⽤
public class Main {

    public static void main(String[] args) throws Exception {
        List<Student> list = Arrays.asList(new Student(32), new Student(33), new Student(21), new Student(29), new Student(18));

        // list.stream().max(Comparator.comparingInt(Student::getAge));

        // 获取最大
        Optional<Student> optionalStudent =  list.stream().max((s1, s2)->{
            return Integer.compare(s1.getAge(),s2.getAge());
        });

        // 获取最小
        Optional<Student> optionalStudent =  list.stream().min((s1, s2)->{
            return Integer.compare(s1.getAge(),s2.getAge());
        });

        // 打印
        Student student = optionalStudent.get();
        System.out.println(student.getAge());
    }

}

class Student {

    private int age;

    public Student() {
    }

    public Student(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
posted @ 2022-08-24 11:45  DogLeftover  阅读(54)  评论(0编辑  收藏  举报