Java8新特性中的常用Stream流操作

一些流操作的示例:

        // 1.创建一个无限流, 2.限制流元素的个数
        List<String> echos = Stream.generate(() -> "Echo").limit(20).collect(Collectors.toList());
        System.out.println(echos);
        System.out.println(echos.size());
      	//[Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo, Echo]
		//20
        // 1.创建一个无限流, 2.限制流元素的个数
        List<Double> collect = Stream.generate(Math::random).limit(100).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println(collect.size());
        //[0.7236886131294109, 0.3904887138215575, 0.7574771560604795, 0.6580281140991987, 0.44218139797859934, 0.13358353806183754,.....,0.8279386336027039]
		//100
        // 1.流中数据乱序排列 2.限制流元素的个数 3. 返回新的流
        List<Double> collect1 = collect.stream().sorted((o1,o2) -> new Random().nextInt(2) - 1).limit(10).collect(Collectors.toList());
        System.out.println(collect1);
        System.out.println(collect1.size());
        //[0.7574771560604795, 0.8228541705924282, 0.6580281140991987, 0.5622586711391435, 0.4332448121266945, 0.04635639350844134, 0.13358353806183754, 0.5581678357163088, 0.25973900944994166, 0.3904887138215575]
		//10
        // 创建一个序列,seed:初始值。UnaryOperate函数根据上一个值生成一个新的元素。限制流元素的个数
        List<Integer> xl = Stream.iterate(10, (pre) -> pre + 1).limit(20).collect(Collectors.toList());
        System.out.println(xl);
        System.out.println(xl.size());
        //[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
		//20
        // 创建一个序列,seed:初始值。predicate:断言是否要继续生成刘元苏。UnaryOperate函数根据上一个值生成一个新的元素。限制流元素的个数
        List<Integer> xl2 = Stream.iterate(10, pre -> pre < 30, pre -> pre + 1).collect(Collectors.toList());
        System.out.println(xl);
        System.out.println(xl2.size());
        //[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
		//20
        // 一种字符串分割成流
        List<String> splitAsStream = Pattern.compile("\\\\PL+").splitAsStream("hello\\PLLL1234\\PLword").collect(Collectors.toList());
        System.out.println(splitAsStream);
        System.out.println(splitAsStream.size());
		//[hello, 1234, word]
		//3
        // 流元素是文件所有行的内容
        List<String> lines = Files.lines(Path.of("D:\\Program Files (x86)\\study\\dubbo\\007-zk-multi-consumer\\src\\main\\java\\com\\yyl\\consumer\\controller\\UserController.java")).collect(Collectors.toList());
        System.out.println(lines);
        System.out.println(lines.size());
		//[package com.yyl.consumer.controller;, import com.yyl.api.entity.User;, ......,     public User getUserInfoV2(@PathVariable int userId) {,         return userServiceV2.getUserById(userId);,     }, }]
		//32
        // 连接两个流
        List<String> concat = Stream.concat(splitAsStream.stream(), lines.stream()).collect(Collectors.toList());
        System.out.println(concat);
        System.out.println(concat.size());
        //[hello, 1234, word, package com.yyl.consumer.controller;, , import com.yyl.api.entity.User;, import com.yyl.api.service.UserService;,.....,return userServiceV2.getUserById(userId);,     }, }]
		//35
        // 输入分离后转化为流
        List<String> tokens = new Scanner("hello java  word, I Love You").tokens().collect(Collectors.toList());
        System.out.println(tokens);
        System.out.println(tokens.size());
        //[hello, java, word,, I, Love, You]
		//6
        // flatMap,扁平化Map
        List<String> flatList = tokens.stream().flatMap(child -> Arrays.stream(child.split(""))).collect(Collectors.toList());
        System.out.println(flatList);
        System.out.println(flatList.size());
		//[h, e, l, l, o, j, a, v, a, w, o, r, d, ,, I, L, o, v, e, Y, o, u]
		//22
        // 第一个元素无用,跳过第一个
        List<String> clippedTokens = tokens.stream().skip(1).collect(Collectors.toList());
        System.out.println(clippedTokens);
        System.out.println(clippedTokens.size());
		//[java, word,, I, Love, You]
		//5
        // 自开始执行,takeWhile中的断言为true时保留当前元素,predicate为false时,流中后面的元素全部舍弃,然后返回新的流
        List<String> obtainUntilPredicateFalse = flatList.stream().takeWhile("helloILoveYou"::contains).collect(Collectors.toList());
        System.out.println(obtainUntilPredicateFalse);
        System.out.println(obtainUntilPredicateFalse.size());
		//[h, e, l, l, o]
		//5
        // 自开始执行,dropWhile中的断言为true时舍弃当前元素,predicate为false时,流中后面的元素全部保留,然后返回新的流
        List<String> obtainUntilPredicateTrue = flatList.stream().dropWhile("helloILoveYou"::contains).collect(Collectors.toList());
        System.out.println(obtainUntilPredicateTrue);
        System.out.println(obtainUntilPredicateTrue.size());
        //[j, a, v, a, w, o, r, d, ,, I, L, o, v, e, Y, o, u]
		//17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值