java1.8关于 List .Stream()常用的方法

  • List <String>list=Arrays.strean(array).collect(Collectors.toList());//数组转集合

  • String[] array=list.stream().toArray(String[]::new);//集合转数组

  • Map<String,String>list.stream().collect(Collectors.toMap(Student::getId,Student::getName));//集合转Map

  • Map<String,Student>list.stream().collect(Collectors.groupingBy(Student::getId));//集合转Map(没有重复的key时

  • Map<String,Student>list.stream().collect(Collectors.groupingBy(Student::getId,(v1,v2)->v2));//集合转Map(有重复的key时,后面的覆盖前面的)

  • List <String>newList2=llist.stream().map(Student::getId)).collect(Collectors.toList());//对Student集合对象中的id进行分组,生成一个集合

  • List<Student>list2=list.stream().filter(item.getId().equals."5").collect(Collectors.toList());//筛选符合条件的元素生成一个集合

  • Long count = listUser.stream().filter(user -> user.getId().equals("2")).collect(Collectors.counting());//筛选符合条件的元素个数

  • List <String>list=new ArrayList<>();

  • list.removeIf(item->"22".equals(item));//在原有的list上剔除某个元素

  • list.removeIf(item->item.contains("1"));//在原有的list上剔除包含某个关键字的元素

  • boolean flag=list.stream().distanct().count()==list.size();//判断集合是否存在重复数据

  • List <String>newList2=list.stream().distanct().collect(Collectors.toList());//剔除集合中的重复元素

  • List <String>newList1=list.stream().filter(Object::nonNull).collect(Collectors.toList());//剔除集合中null值的元素

  • boolean flag=list.removeAll(Collections.singletion(null));//剔除集合中null值的元素,剔除成功会返回true,否则返回false,如果集合中没有null,返回值为false

  • List <String>newList=list.stream().filter(item->!"2".equals(item)).collect(Collectors.toList());//剔除list中的一个元素,生成新的集合

  • List <String>newList=list.stream().filter(item->!"2".contains(item)).collect(Collectors.toList());//剔除list中包含某个元素的操作

  • List<List<String>>list1=Arrays.asLIst(Arrays.asLIst("1","2"),Arrays.asLIst("3","4"));//将多个集合组成到一个集合数组中,[[1,2],[3,4]]

  • List <String> newList=list1.stream().flatMap(Collection::stream).collect(Collectors.toList());///将多个集合合并到一个集合中[1,2,3,4]

  • List<String>newList2=list.stream().sorted(Comparing(Student::getId))).collect(Collectors.toList());//根据对象中的某个属性从小到大排序

  • List <String>newList3=list.stream().limit(3).collect(Collectors.toList());//限制输出集合当中3个元素

  • List <String>newList3=list.stream().skip(3).collect(Collectors.toList());//跳过前3个元素

  • String s=list.stream().reduce((s1,s2)->Double.parseDouble(s1)>Double.parseDouble(s2)?s1:s2).get();//找出list集合中的最大值

  • String s1=list.stream().max(Comparator.comparing(Student::getId)).get().getId();//找出list对象集合中的最大值

  • long count = list.stream().mapToInt(item -> item.getAge()).summaryStatistics().getSum();//求list对象某个属性的和

  • String join = String.join(",", list);String collect = list.stream().collect(Collectors.joining(":"));//将集合元素按逗号拼接起来

  • String collect =list.stream().reduce("",(a,b->a+b));//将集合元素拼接起来

  • boolean z = list.stream().anyMatch(item -> item.startsWith("z"));//判断集合是否存在以某个字符开头的元素

  • List<String> list2 = Optional.ofNullable(list1).orElse(list2);//list为null则返回list2

相关推荐
尘觉1 天前
中秋节与 Spring Boot 的思考:一场开箱即用的团圆盛宴
java·spring boot·后端
Le1Yu1 天前
2025-10-7学习笔记
java·笔记·学习
popoxf1 天前
spring容器启动流程(反射视角)
java·后端·spring
谷哥的小弟1 天前
Spring Framework源码解析——ApplicationContextAware
spring·源码
AAA修煤气灶刘哥1 天前
监控摄像头?不,我们管这个叫优雅的埋点艺术!
java·后端·spring cloud
寻星探路1 天前
Java EE初阶启程记09---多线程案例(2)
java·开发语言·java-ee
武子康1 天前
Java-141 深入浅出 MySQL Spring事务失效的常见场景与解决方案详解(3)
java·数据库·mysql·spring·性能优化·系统架构·事务
珹洺1 天前
Java-Spring入门指南(十五)SpringMVC注解开发
java·spring·microsoft
小满、1 天前
什么是Maven?关于 Maven 的坐标、依赖管理与 Web 项目构建
java·maven
半旧夜夏1 天前
【设计模式】核心设计模式实战
java·spring boot·设计模式