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

相关推荐
代码or搬砖12 分钟前
String字符串
android·java·开发语言
AM越.2 小时前
Java设计模式详解--装饰器设计模式(含uml图)
java·设计模式·uml
5980354152 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL2 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者2 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言
从心归零3 小时前
springboot-jpa的批量更新方法
java·spring boot·spring
这周也會开心3 小时前
128陷阱,==与equals区别
java·开发语言
LiamTuc3 小时前
Hibernate‌
spring
TAEHENGV3 小时前
回收站模块 Cordova 与 OpenHarmony 混合开发实战
android·java·harmonyos
a努力。4 小时前
宇树Java面试被问:方法区、元空间的区别和演进
java·后端·面试·宇树科技