123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package J20250724.demo02_function_program;
- import java.util.ArrayList;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo01_stream
- * @description
- * @create 2025/7/24
- */
- public class Demo01_stream {
- public static void main(String[] args) {
- ArrayList<String> list=new ArrayList<>();
- list.add("万事如意");
- list.add("万家灯火");
- list.add("万JL");
- list.add("万达");
- list.add("张三");
- list.add("张老三");
- list.add("万元户");
- list.add("万宝路");
- //把所有万开头的元素放到一个新的集合
- // ArrayList<String> list1=new ArrayList<>();
- // for (String s: list){
- // if (s.startsWith("万")){
- // list1.add(s);
- // }
- // }
- //
- // //把万开头的集合中长度为3的元素存储到一个新的集合
- // ArrayList<String> list2=new ArrayList<>();
- // for (String s:list1){
- // if (s.length()==3){
- // list2.add(s);
- // }
- // }
- // System.out.println(list2);
- //通过stream流的方式,获取万开头并且长度为3的元素,遍历出来
- list.stream()//获取流 建立流水线
- .filter(s->s.startsWith("万")) //过滤万字开头 罐装
- .filter(s->s.length()==3) //过滤长度为3 //盖盖儿
- .forEach(s-> System.out.println(s)); //遍历集合 最后一步操作 装修
- }
- }
|