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

相关推荐
q***61507 分钟前
eclipse配置Spring
java·spring·eclipse
Miraitowa_cheems22 分钟前
LeetCode算法日记 - Day 106: 两个字符串的最小ASCII删除和
java·数据结构·算法·leetcode·深度优先
q***581924 分钟前
Spring全家桶简介
java·后端·spring
武昌库里写JAVA36 分钟前
微擎服务器配置要求,微擎云主机多少钱一年?
java·vue.js·spring boot·后端·sql
IUGEI1 小时前
深入解析HTTP长连接原理
java·网络·后端·网络协议·tcp/ip·http·https
q***64971 小时前
头歌答案--爬虫实战
java·前端·爬虫
凌波粒1 小时前
SpringMVC基础教程(4)--Ajax/拦截器/文件上传和下载
java·前端·spring·ajax
汤姆yu1 小时前
基于springboot的电脑商城系统
java·spring boot·后端
未若君雅裁1 小时前
LeetCode 51 - N皇后问题 详解笔记
java·数据结构·笔记·算法·leetcode·剪枝