jkd8新特性 StreamAPi流

问题:

在对集合进行处理的时候,我们需要经常对集合进行遍历,然后创建新的集合装载数据,比较麻烦;

使用jdk8提供的Stream流进行处理就会比较方便了;

List<String> strings = Arrays.asList("张张三", "张三", "王五");

        //普通方法进行过滤

        ArrayList<String> strings1 = new ArrayList<>();
        for (String str: strings) {
            if (str.length()>2){
                strings1.add(str);
            }
        }
        //普通方法输出list集合
        for (String s : strings1) {
            System.out.println(s);
        }
        System.out.println("======");
        //使用Stream流和lambda表达式进行输出和过滤
        strings.stream().filter(s->s.length() ==3).forEach(s-> System.out.println(s));

#知识点

1.Stream和io流没有关系;

2.Stream不是一个数据结构,不存放数据;

3.在stream流中可以比作是一个流水线,主要是用来对集合的数据进行处理的;

#Stream获取流的方式:

1.在Collection集合中的对象可以来获取Stream对象(map中没有实现Collection接口),

不过可以分别得到map集合中的KeyS,values来获取Stream流;

   HashMap<String, String> stringStringHashMap = new HashMap<>();
        Stream<String> stream = stringStringHashMap.keySet().stream();
        Stream<String> stream1 = stringStringHashMap.values().stream();

2.对数组进行处理

        //对数组进行操作
        //第一种
        Stream<String> stringStream = Stream.of("1", "2", "3");
        //第二种
        String[] strs = new String[]{"4", "4", "6"};
        Stream<String> strs1 = Stream.of(strs);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值