JavaSE-Stream流的终结方法_forEach_count_toArray_collect

目录

Stream流的终结方法

forEach

count

toArray

collect


Stream流的终结方法

forEach

public class StreamDemo12 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
        list1.stream().forEach(s-> System.out.println(s));
    }
}

打印结果:

count

public class StreamDemo13 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
        System.out.println(list1.stream().count());
    }
}

打印结果:

toArray

public class StreamDemo14 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
        //这种方式创建的数组的元素类型为Object
        Object[] arr1 = list1.stream().toArray();
        System.out.println(Arrays.toString(arr1));
        //这种方式创建的数组的元素类型为String
        String[] arr2 = list1.stream().toArray(value -> new String[value]);
        System.out.println(Arrays.toString(arr2));
    }
}

打印结果:

collect

由于collect的讲解内容过多,所以我单独写了一篇文章,如果想了解collect的相关知识,请阅读这篇文章:JavaSE-Stream流的终结方法_collect-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值