java8新特性与springboot结合使用
2019-08-01 11:20
344 查看
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
前言
今天和大家探讨java8新特性,主要从以下两方面stream、foreach来讲述相关的使用方法,主要涉及了stream流中的filter(过滤),groupingBy (分组),distinct(去重),根据某种属性去重…等
实体类
package com.example.student.entity; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Getter; import lombok.Setter; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; /** * @Desc 学生实体类 */ @Table(name = "student") @Entity @Setter @Getter @JsonInclude(JsonInclude.Include.NON_NULL) public class Student implements Serializable { @Id @GeneratedValue private Integer id; private String name; private Integer age; private String grade; }
repository层
package com.example.student.repository; import com.example.student.entity.Student; import org.springframework.data.repository.CrudRepository; import java.util.Collection; /** * sql语句 */ public interface StudentRepository extends CrudRepository<Student,Integer> { Integer deleteAllByIdIn(Collection<Integer> ids); //根据id集合找学生集合 List<Student> findByIdIn(Collection<Integer> ids); }
service层
这里主要写stream流和foreach的使用方法
package com.example.student.service; import com.example.student.entity.Student; import com.example.student.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collection; /** * 业务层 */ @Service public class StudentServiceImpl { @Autowired private StudentRepository studentRepository; /** * java8新特性的使用方法 * * @param ids * @return */ public void test(Collection<Integer> ids) { List<Student> students = studentRepository.findByIdIn(ids); if (students.size() == 0) { return; } // 1.1 将list转成stream流,并使用filter找出年龄大于18岁的学生 List<Student> student1 = students.stream().filter(x -> x.getAge() > 18) .collect(Collectors.toList()); // 1.2 将list转成stream流,并使用filter找出年龄大于18岁的学生id集合--方法一 List<Integer> id1 = student1.stream().map(Student::getId).collect(Collectors.toList()); // 1.3 将list转成stream流,并使用filter找出年龄大于18岁的学生id集合--方法二 List<Integer> id2 = students.stream().filter(x -> x.getAge() > 18).map(Student::getId) .collect(Collectors.toList()); // 1.4 根据学生姓名分组 Map<String, List<Student>> student2 = students.stream().collect(Collectors.groupingBy(Student::getName)); // 1.5 根据学生姓名分组统计人数 Map<String, Long> studentNum = students.stream().collect(Collectors.groupingBy(Student::getName , Collectors.counting())); // 1.6 根据学生姓名和年龄分组 Map<String, Map<Integer, List<Student>>> student3 = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getAge))); // 1.7 根据学生姓名去重 List<Student> userRoles = students.stream().collect( Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)); // 1.8 根据student整体去重 List<Student>students1 = students.stream().distinct().collect(Collectors.toList()); // 1.9 根据分组的key值对结果进行排序、放进另一个map中 Map<String, List<Student>> map = new HashMap<>(); student2.entrySet().stream().sorted(Map.Entry.<String, List<Student>>comparingByKey() .reversed()) .forEachOrdered(x -> map.put(x.getKey(), x.getValue())); // 2.0 foreach students.forEach(y -> { Map<String, Object> result = new HashMap<>(); // 根据需求做相关逻辑判断 ....... }); } }
相关文章推荐
- 数据库存储父、子节点 使用递归算法结合数据库解析成java树形结构数据(SpringBoot+SSM整合)
- springboot结合redis使用CachingConfigurerSupport方法不能被继承
- 爬虫框架webmagic与spring boot的结合使用
- Spring Boot使用Spring Data JPA结合EntityManager实现多表关联
- Spring Boot 使用Java代码创建Bean并注册到Spring中
- java springboot与elasticsearch结合以及elasticsearch增删改查实例
- spring-boot 结合spring-data-redis使用redis的消息队列
- Java SpringBoot中使用sqljdbc4注意事项 java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
- 41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】
- 以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ
- springboot+redis(注解方式)使用(java代码部分)
- 使用Java(Springboot)操作Redis
- springboot 2.0 使用Hikari连接池(号称java平台最快的,替换druid)
- 使用springboot连接druid,Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
- java代码操作MongoDB springboot与mg结合实现CRUD操作
- 使用Java(Springboot)操作Redis
- 爬虫框架webmagic与spring boot的结合使用
- 结合SPRING 2.0使用JAVA PERSISTENCE API
- 结合SPRING 2.0使用JAVA PERSISTENCE API
- java鬼混笔记:springboot之thymeleaf 2:字符串Strings常见的使用方法