java8 Stream 使用案例
java8_Stream
- 预定义示例参数
- filter
- map
- mapToInt
- mapToLong
- mapToDouble
- peek
- flatMap
- flatMapToInt
- distinct
- sorted
- limit
- forEach
- forEachOrdered
- reduce
- minAMax
- count
- match
- stream静态
预定义示例参数
static class TestUser {private Integer id;private String name;private String mobile;public TestUser(){}public TestUser(String name, String mobile) {this.id = new Random().nextInt(Integer.MAX_VALUE);this.name = name;this.mobile = mobile;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}@Overridepublic String toString() {return "TestUser{" +"id=" + id +", name='" + name + '\'' +", mobile='" + mobile + '\'' +'}';}}
List<TestUser> userList = Arrays.asList(new TestUser("字符串1","114"),new TestUser("字符串2","125"),new TestUser("字符串3","136"),new TestUser("a^z","147"),new TestUser("0~9","158"),new TestUser("A^Z","169"));List<String> list = Arrays.asList("字符串1","字符串22","字符串333","a^z","0~9","A^Z");
filter
/*** filter : 条件过滤 类似于if,过滤后还是当前流* @param list*/public static void streamFilter(List<String> list){System.out.println("**********************filter**************************");Stream<String> stringStream = list.stream().filter(c -> c.equals("字符串1"));stringStream.findFirst().ifPresent(System.out::println);}
map
/*** 接收一个函数作为参数,此函数作用到Stream中每一个元素,形成一个新的元素,所有新的元素组成一个新的流。* map : 使用map过滤的内容,map 中 使用的是当前该对象通用的内容,可以转成map集合 通用类型(共性),或者转成数据流。* @param list*/public static void streamMap(List<String> list){System.out.println("**********************map**************************");// 转为数据流Stream<Integer> integerStream = list.stream().map(String::length);// 转为list对象List<Integer> collectList = list.stream().map(String::length).collect(Collectors.toList());// 转为map 当前的示范为错误数据,map的唯一key不能重复,否则在赋值时会产生 Duplicate key
// Map<Integer, String> collectMap = list.stream().map(String::length).collect(Collectors.toMap(c -> c.intValue(), k -> k.toString()));System.out.println("转为数据流输出");integerStream.findAny().ifPresent(System.out::println);System.out.println("转为list对象");collectList.forEach(System.out::println);}
mapToInt
/*** mapToInt 使用当前方法是内置方法结果返回流数据必须是int类型,返回为 IntStream 类型,可再次作其他操作。* 强转类型一定要注意内容是否存在非强转类型* @param list 演示*/public static void mapToInt(List<TestUser> list){System.out.println("**********************mapToInt 或 可向下强转 **************************");IntStream intStream = list.stream().mapToInt(c -> c.id);intStream.forEach(System.out::println);}
mapToLong
/*** mapToLong 使用当前方法是内置方法结果返回流数据必须是Long 或 可向下强转 类型,返回为 LongStream 类型,可再次作其他操作。* 强转类型一定要注意内容是否存在非强转类型* @param list 演示*/public static void mapToLong(List<TestUser> list){System.out.println("**********************mapToLong**************************");LongStream longStream = list.stream().mapToLong(c -> c.id);longStream.forEach(System.out::println);}
mapToDouble
/*** mapToDouble 使用当前方法是内置方法结果返回流数据必须是Double 或 可向下强转 类型,返回为 DoubleStream 类型,可再次作其他操作。* 强转类型一定要注意内容是否存在非强转类型* @param list 演示*/public static void mapToDouble(List<TestUser> list){System.out.println("**********************mapToDouble**************************");DoubleStream stream = list.stream().mapToDouble(c -> c.id);stream.forEach(System.out::println);}
peek
/*** 中间操作 对数据进行加工或者展示,操作的流对象没有更换。* 当前方法 peek 就是中间操作。(终止操作:即将当前流数据转换为其他类型数据,对流的当前操作已经结束,可以进行下一次的流操作)* @param list*/public static void peek(List<TestUser> list){System.out.println("**********************peek**************************");list.stream().peek(c -> {// 可以对内附对象最初修正操作或者其他业务操作System.out.println(c);}).collect(Collectors.toList());}
flatMap
/*** 接收一个函数作为参数,它将流中的每个元素都转换成另一个流,然后把所有流再连接形成一个最终流。* @param list*/public static void flatMap(List<TestUser> list){System.out.println("**********************flatMap**************************");List<List<TestUser>> flatList = Arrays.asList(list);flatList.stream().flatMap(c -> c.stream().distinct()).collect(Collectors.toList()).forEach(c -> System.out.println(c));}
flatMapToInt
/*** 操作的流数据必须返回的是int类型的流内容* flatMapToLong* flatMapToDouble* 以上同理* @param list*/public static void flatMapToInt(List<TestUser> list){System.out.println("**********************flatMapToInt**************************");List<List<TestUser>> flatList = Arrays.asList(list);flatList.stream().flatMapToInt(c -> c.stream().map(TestUser::getId).mapToInt(Integer::intValue)).forEach(System.out::println);}
distinct
/*** 去重* @param list*/public static void distinct(List<TestUser> list){System.out.println("**********************distinct**************************");Set<Integer> collect = list.stream().peek(c -> {System.out.println("原:" + c.id);c.id = Optional.of(c.id).filter(x -> x > 0 && x % 2 == 0).orElse(123);}).map(TestUser::getId).collect(Collectors.toSet());collect.forEach(System.out::println);// 或者list.stream().peek(c -> {System.out.println("原:" + c.id);c.id = Optional.of(c.id).filter(x -> x > 0 && x % 2 == 0).orElse(123);}).map(TestUser::getId).distinct().collect(Collectors.toList()).forEach(System.out::println);}
sorted
/*** 排序* @param list*/public static void sorted(List<TestUser> list){System.out.println("**********************sorted**************************");System.out.println("升序:");list.stream().sorted(Comparator.comparing(TestUser::getId)).collect(Collectors.toList()).forEach(System.out::println);System.out.println("降序:");list.stream().sorted(Comparator.comparing(TestUser::getId).reversed()).collect(Collectors.toList()).forEach(System.out::println);}
limit
/*** 限制流内容的数量* limit/skip 也不是终止操作,中间操作,操作完成的对象还是当前流本身,只是对数据内容做了处理。** @param list*/public static void limit(List<TestUser> list){System.out.println("**********************limit**************************");// 截取第一个list.stream().limit(1).forEach(System.out::println);// 跳过第一个截取两个list.stream().skip(1).limit(2).forEach(System.out::println);}
forEach
/*** 循环* @param list*/public static void forEach(List<TestUser> list){System.out.println("**********************forEach**************************");list.stream().forEach(System.out::println);}
forEachOrdered
/*** 强制排序 无论是串行流还是并行流使用当前方法都要进行排序* @param list*/public static void forEachOrdered(List<TestUser> list){System.out.println("**********************forEachOrdered**************************");list.stream().forEach(System.out::println);System.out.println("---");list.stream().forEachOrdered(System.out::println);System.out.println("---");list.parallelStream().forEach(System.out::println);System.out.println("---");list.parallelStream().forEachOrdered(System.out::println);}
reduce
/*** 可用于计算,将多个值通过制定业务计算规则为一个值* @param list*/public static void reduce(List<TestUser> list){System.out.println("**********************reduce**************************");List<Integer> arrList = Arrays.asList(1,23,40,60,70,34,35);// 求和list.stream().mapToInt(c -> c.id = new Random().nextInt(1000)).reduce(Integer::sum).ifPresent(System.out::println);// 求最大值list.stream().mapToInt(c -> c.id = new Random().nextInt(1000)).reduce(Integer::max).ifPresent(System.out::println);// 基于一个变量值 与 1001比较 大于展示自身 小于展示1001System.out.println(list.stream().mapToInt(c -> c.id = new Random().nextInt(1000)).reduce(1001, Integer::max));// 基于一个变量值 累加System.out.println(list.stream().mapToInt(c -> c.id = new Random().nextInt(1000)).reduce(1001, Integer::sum));// 求最大值arrList.stream().reduce((x,y) -> x > y ? x : y).ifPresent(System.out::println);arrList.stream().reduce(Integer::max).ifPresent(System.out::println);// 最小值arrList.stream().reduce(Integer::min).ifPresent(System.out::println);// 集合乘积arrList.stream().reduce((x,y) -> x * y).ifPresent(System.out::println);}
minAMax
/*** 最大、最小 、求和* @param list*/public static void minAMax(List<TestUser> list){System.out.println("**********************min&Max**************************");List<Integer> arrList = Arrays.asList(1,23,40,60,70,34,35);arrList.stream().min(Integer::compareTo).ifPresent(System.out::println);arrList.stream().max(Integer::compareTo).ifPresent(System.out::println);System.out.println(arrList.stream().mapToInt(c -> c.intValue()).sum());}
count
/*** count 计数* @param list*/public static void count(List<TestUser> list){System.out.println("**********************count**************************");List<Integer> arrList = Arrays.asList(1,23,40,60,70,34,35);System.out.println(arrList.stream().count());}
match
/*** anyMatch 只要有一个匹配 true* allMatch 必须所有都匹配 true* noneMatch 不能又一个匹配 true* @param list*/public static void match(List<TestUser> list){System.out.println("**********************xx**************************");List<Integer> arrList = Arrays.asList(1,23,40,60,70,34,35);System.out.println(arrList.stream().anyMatch(c -> c > 2));System.out.println(arrList.stream().allMatch(c -> c > 2));System.out.println(arrList.stream().noneMatch(c -> c > 2));}
stream静态
/*** stream 静态方法*/public static void stream(){System.out.println("**********************Stream**************************");Stream<Object> empty = Stream.empty();Stream<Object> build = Stream.builder().build();Stream<Integer> integerStream1 = Stream.of(1, 2, 3, 4, 6);Stream<Integer> integerStream2 = Stream.of(1, 2, 3, 4, 6);Stream<String> stringStream = Stream.of("1", "2", "3", "4");// 根据规则生成一些内容 输出10 个奇数Stream.iterate(1, c -> c + 2).limit(10).forEach(System.out::println);Stream<UUID> limit = Stream.generate(UUID::randomUUID).limit(2);Stream<TestUser> generate = Stream.generate(TestUser::new).limit(3);System.out.println("-------------");// 不去重 合并相同规则内容Stream.concat(integerStream1, integerStream2).distinct().forEach(System.out::println);}
加油!!!
java8 Stream 使用案例相关推荐
- java8 stream案例分析
java8 stream Stream是java8 推出的一个处理集合类的一个全新的接口,在工作当中经常用到,并且他的预发规则与我们平台的有一点不一样,是流式的处理,非常类似RXJava的一个风格. ...
- java双层list扁平化,浅谈java8 stream flatMap流的扁平化操作
概念: Steam 是Java8 提出的一个新概念,不是输入输出的 Stream 流,而是一种用函数式编程方式在集合类上进行复杂操作的工具.简而言之,是以内部迭代的方式处理集合数据的操作,内部迭代可以 ...
- Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合
Java8 Stream 1 Stream概述 2 Stream的创建 3 Stream的使用 案例使用的员工类 3.1 遍历/匹配(foreach/find/match) 3.2 筛选(filter ...
- 20个实例玩转Java8 Stream
20个实例玩转Java8 Stream 20个实例玩转Java8 Stream Stream概述 Stream的创建 stream和parallelStream的简单区分 stream的使用 遍历/匹 ...
- java8 stream流 将一个list转换成list
java8 stream流 将一个对象集合转换成另一个对象集合 案例一: // 利用stream进行类型转化 List<String> stringList = new Array ...
- Java8 Stream 流的创建、筛选、映射、排序、归约、分组、聚合、提取与组合、收集、接合、foreach遍历
目录 一 了解Stream 1 Stream概述 那么什么是Stream? Stream可以由数组或集合创建 Stream有几个特性: Stream流的起始操作 2 Stream的创建----Str ...
- JAVA8 Stream方法使用详解reduce、IntStream(二)
文章目录 一 归约 1.元素求和 2.最大值和最小值 二.数值流 1.映射数值流 2.转换对象流 3.数值范围 三.构建流 1.由值创建流 2.由数组创建流 3.由文件生成流 4.由函数生成流 此章节 ...
- 怎么break java8 stream的foreach
文章目录 简介 使用Spliterator 自定义forEach方法 总结 怎么break java8 stream的foreach 简介 我们通常需要在java stream中遍历处理里面的数据,其 ...
- java多字段排序,java8 stream多字段排序的实现
很多情况下sql不好解决的多表查询,临时表分组,排序,尽量用java8新特性stream进行处理 使用java8新特性,下面先来点基础的 List list; 代表某集合 //返回 对象集合以类属性一 ...
最新文章
- 清华博士接亲被要求现场写代码,5 分钟做出一颗爱心
- 早上醒来收获一个Surprise,成为CSDN博客专家了
- yield的用法详解
- 关于tensorflow和keras那些事儿
- js+css立体旋转
- 《Effective.Enterprise.Java中文版》知识点摘要
- Python学习:命令行运行,循环结构
- 三相锁相 c 语言 程序,轻松玩转DSP——基于TMS320F2833x(Word+PDF+ePub+PPT)
- webform计算某几列结果_工业CAE案例实战精选|脱硫吸收塔工艺仿真计算系统
- idea创建svn分支
- 一文十大排序算法(动画图解)
- dB、dBFS、dBV、dBu...都是啥啊..
- 空间战场态势感知系统
- ASPack压缩可执行文件
- python 存根_如何用Python编写类方法的存根
- Titantic乘客生还预测数据分析报告—基于python实现
- echarts中y轴设置刻度_xAxis 配置
- 使用 Python 生成二维码
- 设置img标签的默认图片
- The Copernicus Global Land Service (CGLS)账号注册与数据下载
热门文章
- [Android]从canDrawOverlays权限获取错误说起
- 计算机主页为什么打不开怎么办,电脑输入192.168.1.1管理页面打不开怎么办
- 中文版Ubuntu系统转为英文版Ubuntu
- regopenkeyexfailed什么意思_外置网卡驱动安装出现RegOpenKeyEx Failed,怎么回事?
- 用eclipse读spark源码
- VirtualBox虚拟机安装和环境搭建
- java获取钉钉登录信息,JAVA maven项目使用钉钉SDK获取token、用户
- 怎么做 Satellite assemblies
- c# word文档基本操作 (上)
- docker启动mysql失败(闪退)原因