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

相关推荐
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu2 小时前
Spring MVC消息转换器
java·spring
乔冠宇2 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ3 小时前
简单的SQL语句的快速复习
java·数据库·sql
komo莫莫da3 小时前
寒假刷题Day19
java·开发语言
小小虫码3 小时前
项目中用的网关Gateway及SpringCloud
spring·spring cloud·gateway
S-X-S4 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq84 小时前
设计模式学习(二)
java·学习·设计模式
桦说编程5 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦5 小时前
Redis 基础命令
java·数据库·redis