java8 常用代码 2020-02-26 字数统计: 778 | 阅读时长≈ 4 分钟 1. 使用java8 提取出 list 中 bean 的某一属性2. 使用 java8 提取出 list 中 bean 的两个属性转换为Map3. 使用 java8 根据某个属性倒序排序3. List 转 Set4. java8 中根据判断删除列表list中的元素5. java8 获取 lsit 中bean 的某一属性最大的对象6. java8 计算代码运行时间 1. 使用java8 提取出 list 中 bean 的某一属性1234567891011121314151617181920212223242526272829COPYpublic static void main(String[] args) { List<Student> stuList = new ArrayList<Student>(); Student st1 = new Student("123","aaa"); Student st2 = new Student("234","bbb"); Student st3 = new Student("345","ccc"); Student st4 = new Student("345","ccc"); stuList.add(st1); stuList.add(st2); stuList.add(st3); stuList.add(st4); //1.提取出list对象中的一个属性 List<String> stIdList1 = stuList.stream() .map(Student::getId) .collect(Collectors.toList()); stIdList1.forEach(s -> System.out.print(s+" ")); System.out.println(); System.out.println("----------"); //2.提取出list对象中的一个属性并去重 List<String> stIdList2 = stuList.stream() .map(Student::getId).distinct() .collect(Collectors.toList()); stIdList2.forEach(s -> System.out.print(s+" ")); /* 结果: 123 234 345 345 ---------- 123 234 345 */} 2. 使用 java8 提取出 list 中 bean 的两个属性转换为Map123COPYList<DictVO> dictVOList = tSysDictService.findByDictType("dataset_catalog_icon");Map<String, String> iconMap = dictVOList.stream() .collect(Collectors.toMap(DictVO::getText, DictVO::getValue)); 3. 使用 java8 根据某个属性倒序排序12COPYlist = list.stream().sorted(Comparator.comparing(PromotionForHomeDto::getCreateTime).reversed()) .collect(Collectors.toList()); 3. List 转 SetJava8 List<对象> 转 Set、Map(高级)、排序、分组、统计 12345COPYSystem.out.println("----------------普通List---------------");normalList.forEach(System.out::println);System.out.println("----------------普通List转Set---------------");Set<Integer> normalSet = normalList.stream().map(Student::getClassNo).collect(Collectors.toSet());normalSet.forEach(System.out::println); 输出: 1234567891011121314COPY----------------普通List---------------Student{id=1, name='Emma', score='A', classNo=701}Student{id=2, name='Larissa', score='S', classNo=701}Student{id=3, name='Sophia', score='B', classNo=701}Student{id=4, name='Ashley', score='B', classNo=702}Student{id=5, name='May', score='C', classNo=702}Student{id=6, name='Hailey', score='D', classNo=702}Student{id=7, name='Kelly', score='S', classNo=703}Student{id=8, name='Amy', score='A', classNo=703}Student{id=9, name='Wesley', score='C', classNo=703}----------------普通List转Set---------------701702703 4. java8 中根据判断删除列表list中的元素我们知道,在java8出lambda表达式之前,是不支持在循环中直接对list进行删除的。但是java8的新特性lambda表达式,帮我们实现了这个功能: 1234567COPYList<String> lists = new ArrayList<>();lists.add("a");lists.add("b");lists.add("c");//使用removeIf方法,->里的是判断条件,如果符合这个条件就删除。这里会删除带有c的元素lists.removeIf(s -> s.contains("c")); java8新特性大大的简化了我们的代码,使用更加的方便。 5. java8 获取 lsit 中bean 的某一属性最大的对象1COPYStudent student = studentList.stream().max(Comparator.comparing(Student::getClassCount)).get(); 6. java8 计算代码运行时间java8之前是这样计算代码运行时间的 1234COPYlong startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 long endTime=System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间: "+(end-start)+"ms"); java8之后可以使用Instant获取时间戳来计算代码运行时间 12345678910111213141516COPY public static void main(String[] args) { Instant start = Instant.now(); LongStream.rangeClosed( 0,110 ) //并行流 .parallel() .reduce( 0,Long::sum ); LongStream.rangeClosed( 0,110 ) //顺序流 .sequential() .reduce( 0,Long::sum ); Instant end = Instant.now(); System.out.println("耗费时间"+ Duration.between( start,end ).toMillis());} 打赏 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处! 上一篇 你必须知道的23个最有用的Elasticseaerch检索技巧 下一篇 Redis 数据类型及应用场景