java8 集合结合steam操作实例

集合框架介绍:https://www.runoob.com/java/java-collections.html

java8-streams:https://www.runoob.com/java/java8-streams.html

介绍不过说,上面两个链接可以看一下。正常情况下,for循环就够用了,但是有时候你需要对集合遍历多次才能完成的事情,使用steam进行操作,可能只要一次就可以了,而且代码更加简洁明了。部分示例直接复制自菜鸟教程

具体实例

下面是测试的对象和测试用的公共代码

// 用于测试的对象
public class Student {private Integer id; //学生编号private String name; // 学生名称private int age;    // 学生年龄private String cla; // 学生班级private int sex;    // 学生性别 0 女 1 男public Student(Integer id, String name, int age, String cla, int sex) {this.id = id;this.name = name;this.age = age;this.cla = cla;this.sex = sex;}... set和get省略@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", cla='" + cla + '\'' +", sex=" + sex +'}';}
}// 测试的公共代码Student student1 = new Student(1, "张1", 12, "一班", 1);Student student2 = new Student(2, "张2", 13, "一班", 0);Student student3 = new Student(3, "张3", 12, "一班", 1);Student student4 = new Student(4, "张4", 14, "二班", 1);Student student5 = new Student(5, "张5", 13, "二班", 0);Student student6 = new Student(6, "张6", 12, "二班", 1);Student student7 = new Student(7, "张7", 11, "三班", 1);Student student8 = new Student(8, "张8", 12, "三班", 0);Student student9 = new Student(9, "张9", 13, "三班", 1);// 异常数据Student student10 = new Student(9, "张9", 13, null, 1);Student student11 = new Student(9, "张9", 13, "", 0);ArrayList<Student> studentList = new ArrayList<>();studentList.add(student1);...........studentList.add(student11);

stream筛选操作

        /*** stream 筛选操作一班用于其他操作前对不符合要求的数据进行过滤,当然筛选功能也是可以单独使用的* */List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl",null);List<String> filtered = strings.stream().filter(string -> string != null && !string.isEmpty()).collect(Collectors.toList());System.out.println(filtered);// 会调用到AbstractCollection的toString方法  [abc, bc, efg, abcd, jkl]// 过滤学生中的异常数据List<Student> collect = studentList.stream().filter(student -> student.getCla() != null && !student.getCla().isEmpty()).collect(Collectors.toList());// 如果过滤条件比较复杂,你可以这么写List<Student> collect1 = studentList.stream().filter(student -> {if (student.getCla() != null && !student.getCla().isEmpty()) {return false;}return true;}).collect(Collectors.toList());

foreach

/*** foreach遍历功能* */Random random = new Random();
IntStream limit = random.ints().limit(10);
// 等价于 int[] ints = limit.toArray();Arrays.stream(ints).forEach(System.out::println);
limit.forEach(System.out::println);
studentList.stream().forEach(System.out::println);

limit

limit 方法用于获取指定数量的流。 以下代码片段使用 limit 方法打印出 10 条数据:

Random random = new Random();
IntStream limit = random.ints().limit(10);
// 等价于 int[] ints = limit.toArray();Arrays.stream(ints).forEach(System.out::println);
limit.forEach(System.out::println);
studentList.stream().forEach(System.out::println);

map

map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:

        /*** 通俗一点讲,就是集合中每个元素计算后获取到另一个元素,添加到新的集合中* */List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);// 获取对应的平方数List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());// 下面的代码执行会得到学生年龄的集合List<Integer> collect2 = studentList.stream().map(i -> i.getAge()).collect(Collectors.toList());//复杂操作List<Integer> collect3 = studentList.stream().map(i -> {return i.getAge() + 1;}).collect(Collectors.toList());

sorted

sorted 方法用于对流进行排序。以下代码片段使用 sorted 方法对输出的 10 个随机数进行排序:

Random random = new Random(); random.ints().limit(10).sorted().forEach(System.out::println);

并行(parallel)程序

parallelStream 是流并行处理程序的代替方法。以下实例我们使用 parallelStream 来输出空字符串的数量:

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); // 获取空字符串的数量 long count = strings.parallelStream().filter(string -> string.isEmpty()).count();

flatMap

用于集合合并:https://blog.csdn.net/weixin_41835612/article/details/83713891


Collectors

Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串:

groupingBy

        /*** 根据特定字段的不同,将一个集合分成多个集合, 甚至根据多个字段进一步分成多重集合* 使用时一般要先进行过滤(筛选),避免异常和得到不想要的结果* */// 根据性别对学生进行分组, 此处演示,不考虑筛选Map<Integer, List<Student>> collect4 = studentList.stream().collect(Collectors.groupingBy(Student::getSex));// 根据复杂的条件进行分组 ,此示例 分组后 map的key为 1 和 2Map<Integer, List<Student>> collect6 = studentList.stream().collect(Collectors.groupingBy(item -> {if (item.getAge() > 12) {return 1;}return 2;}));// 对学生通过班级分组,再通过性别分组Map<String, Map<Integer, List<Student>>> collect5 = studentList.stream().filter(student -> student.getCla() != null && !student.getCla().isEmpty()).collect(Collectors.groupingBy(Student::getCla, Collectors.groupingBy(Student::getSex)));

joining

System.out.println("筛选列表: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);

toMap

        /** * Collectors.toMap 将集合中你需要的数据转换成map* */// 将list转成map,key为 学生名称, value为学生对象 ,这里由于有学生名称重复,抛出异常一般不这么使用//Map<String, Student> collect7 = studentList.stream().collect(Collectors.toMap(Student::getName, item -> item));// 此处和上面相同,但是学生名称重复,会使用后面的学生数据覆盖前面的学生数据Map<String, Student> collect8 = studentList.stream().collect(Collectors.toMap(Student::getName, item -> item,(key1, key2) -> key2));// 也可以使用单个字段作为ValueMap<String, String> collect7 = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getCla, (key1, key2) -> key2));// 除了覆盖,你还可以这么操作Map<String, String> collect9 = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getCla, (key1, key2) -> key2 + "," + key1));// 复杂操作studentList.stream().collect(Collectors.toMap(Student::getName, item -> {return item.getSex() + "," + item.getId();    }, (key1, key2) -> key2));

统计

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());public static void testSum() {List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);// 没有起始值时返回为Optional类型Optional<Integer> sumOptional = integers.stream().reduce(Integer::sum);System.out.println(sumOptional.get());// 可以给一个起始种子值Integer sumReduce = integers.stream().reduce(-1, Integer::sum);System.out.println(sumReduce);//直接用sum方法Integer sum = integers.stream().mapToInt(i -> i).sum();System.out.println(sum);}public static void testMax() {//max reduceStream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);Integer maxReduce = integerStream.reduce(Integer.MIN_VALUE, Integer::max);System.out.println(maxReduce);// maxStream<Integer> integerStream1 = Stream.of(1, 2, 3, 4, 5);OptionalInt max = integerStream1.mapToInt(i -> i).max();System.out.println(max.getAsInt());}public static void testReduce() {Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, null});//求集合元素只和Integer result = stream.filter(item -> {return item != null && item !=1 && item != 2;}).reduce(0, Integer::max);System.out.println(result);stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});//求和stream.reduce((i, j) -> i + j).ifPresent(System.out::println);stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});//求最大值stream.reduce(Integer::max).ifPresent(System.out::println);stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});//求最小值stream.reduce(Integer::min).ifPresent(System.out::println);stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});//做逻辑stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});//求逻辑求乘机int result2 = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) ->{return i * j;});Optional.of(result2).ifPresent(System.out::println);}

git操作

1、 下拉指定分支
git clone -b 分支名  git地址2、git版本控制目录,删除不需要的文件(这里删除所有)
3、 添加忽略文件,提交文件到暂存区,推送到远程git status -s 查看文件状态
git add .
git commit -m 'commit'
git push4、 添加新的项目文件
git add .
git commit -m 'commit'
git push

集合拆分

// 代码来自网络(不记得来自那篇博文了), 亲测有效,可以结合steam操作尝试更简便的实现
public static <T> List<List<T>> split(List<T> resList,int count){if(resList==null ||count<1)return  null ;List<List<T>> ret=new ArrayList<List<T>>();int size=resList.size();if(size<=count){ //数据量不足count指定的大小ret.add(resList);}else{int pre=size/count;int last=size%count;// 前面pre个集合,每个大小都是count个元素for(int i=0;i<pre;i++){List<T> itemList=new ArrayList<T>();for(int j=0;j<count;j++){itemList.add(resList.get(i*count+j));}ret.add(itemList);}// last的进行处理if(last>0){List<T> itemList=new ArrayList<T>();for(int i=0;i<last;i++){itemList.add(resList.get(pre*count+i));}ret.add(itemList);}}return ret;}

需要引入guava

        // 例:将集合:list 分割成每个集合25条数据的集合。List<String> list = new ArrayList<>(100);for (int i = 0; i < 100; i++) {list.add("1");}List<List<String>> partition = Lists.partition(list , 25);for (List<String> data : partition) {//执行插入System.out.println(data.size());}

orcale 批量插入操作

你会发现批量操作缺失快很多

// 以下批量操作,操作的数据量不能过大, 毕竟数据传输是有大小限制的,  所以你可能需要对集合进行拆分,拆分注意了,很容易出错,漏了最后一部分int insertBatch(@Param("studentList") List<Student> studentList);<insert id="insertBatch" useGeneratedKeys="false" parameterType="java.util.List">INSERT ALL<foreach collection="studentList" item="student">into student (id, name) VALUES(#{student.id},#{student.name})</foreach>SELECT 1 FROM DUAL</insert>

java8 集合结合steam操作实例相关推荐

  1. Java8集合的一些操作

    整理一些关于Java8集合的操作,有时间再慢慢更新内容 示例类 CustomerInfo @Data public static class CustomerInfo {/*** 客户代码*/priv ...

  2. mysql通过集合查询_MySQL使用集合函数进行查询操作实例详解

    本文实例讲述了MySQL使用集合函数进行查询操作.分享给大家供大家参考,具体如下: COUNT函数 SELECT COUNT(*) AS cust_num from customers; SELECT ...

  3. JAVA8的特性——steam

    在最近的工作中遇到一种情况,需要将多条记录按ID分类,并且进行判断.然后我发现如果在数据从数据库提取这个过程中进行条件过滤的话,会不可避免的导致循环中多次访问数据库.目前因数据的量不是很大,还能接受, ...

  4. PDF.NET数据开发框架实体类操作实例(for PostgreSQL,并且解决自增问题)

    PDF.NET数据开发框架实体类操作实例(MySQL) 的姊妹篇,两者使用了同一个测试程序,不同的只是使用的类库和数据库不同,下面说说具体的使用过程. 1,首先在App.config文件中配置数据库连 ...

  5. do与mysql数据类型对照_dophon-db: dophon框架的数据库模块,支持mysql,sqlite数据库,带有orm持久化功能与链式操作实例,贴近逻辑习惯,支持mysql多数据源配置...

    dophon-db 项目介绍 dophon框架的数据库模块,也可单独作为一个与数据库模块交互的部件 支持mysql数据库连接 支持orm形式的数据操作 如有疑问请发送邮件联系作者:ealohu@163 ...

  6. python编程遍历_Python字典遍历操作实例小结

    本文实例讲述了Python字典遍历操作.分享给大家供大家参考,具体如下: 1 遍历键值对 可以使用一个 for 循环以及方法 items() 来遍历这个字典的键值对. dict = {'evapora ...

  7. 集合类型及其操作(复习)

    集合类型及其操作一.概述集合类型定义集合操作符集合处理方法集合类型应用场景二.集合类型定义集合是多个元素的无序组合集合类型与数学中的集合概念一致集合元素之间无序,每个元素唯一,不存在相同元素集合是多个 ...

  8. Java Redis操作实例

    Java Redis操作实例 (2016-10-04 18:53:51) 转载▼   分类: Java 转自:http://www.cnblogs.com/edisonfeng/p/3571870.h ...

  9. MongoDB 实战教程:数据库与集合的 CRUD 操作篇

    你好,我是悦创. 公众号:AI悦创 简介 MongoDB 是非关系型数据库(NoSQL)的代表之一,它具有高灵活.可水平扩展.高可用等优秀特性,并且它提供了强大又丰富的查询语句使开发者能够随心所欲地操 ...

最新文章

  1. 手机怎么访问kodi_Kodi播放器
  2. php mysql预处理_PHP MySQL 预处理语句
  3. 组合计数 ---- 2020 icpc 上海 The Journey of Geor Autumn(思维划分问题计数+预处理优化)
  4. 洛谷P1372 又是毕业季IP1414 又是毕业季II[最大公约数]
  5. 定域性和实在性之矛盾的世界
  6. css3学习 理论之渐变
  7. 设置eclipse文件的默认打开方式
  8. Parquet格式描述
  9. scut AccountServer 使用mysql
  10. TensorFlow工作笔记003---python异常大全IndentationError: unexpected indent_expected indented block
  11. Luogu P3373 【模板】线段树 2
  12. 打印1到最大的n位数
  13. Netty 源码深度解析(九) - 编码
  14. react 设置背景图片 (等比例显示,不拉伸)
  15. 固若金汤 - PostgreSQL pgcrypto加密插件
  16. miui9android8.0xp框架,MIUI9MIUI10官方8.0/8.1刷入xp框架
  17. android蓝牙协议栈bluedroid分析,安卓系统蓝牙协议栈 bluedroid 的使能
  18. ResNet网络模型
  19. 虚拟机红帽子php,virtualbox新建redhat9.0(红帽子linux)系统
  20. AndroidQ SystemUI之power键灭屏锁屏流程

热门文章

  1. 如何速读?--如何快速阅读
  2. 游戏技术探讨:LOL新版客户端LCU架构探析
  3. QML类型:Item
  4. 利用 python 脚本 PyRdp 设置 RDS 服务器蜜罐 Honeypot
  5. 2018年云南旅游业“揽金”8991亿元人民币
  6. CuteHttpFileServer(文件共享服务器软件)
  7. 微信小程序(应用号)资源汇总整理
  8. SOLIDWORKS软件不仅仅是三维机械设计软件
  9. 计算机如何制作U盘启动盘,U大侠U盘启动盘制作教程_教你如何制作U盘启动 - U大侠,装机专家...
  10. 全志平台H313 移植BCM4354驱动