| 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); |
| } |
| |
| } |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| |
| |
| 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); |
| |
| |
| 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); |
| } |
| |
| } |
| 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()); |
| |
| |
| List<String> resultList = list.stream().sorted(Comparator.comparing(String::length).reversed()).limit(3).collect(Collectors.toList()); |
| System.out.println(resultList); |
| } |
| |
| } |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| List<String> list = Arrays.asList("springboot", "springcloud", "redis", "git", "netty", "java", "html", "docker"); |
| |
| boolean flag1 = list.stream().allMatch(obj->obj.length()>5); |
| System.out.println(flag1); |
| |
| boolean flag2 = list.stream().anyMatch(obj->obj.length()>5); |
| System.out.println(flag2); |
| } |
| |
| } |
| 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)); |
| |
| |
| |
| |
| 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; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术