版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
前言
今天和大家探讨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<>();
// 根据需求做相关逻辑判断
.......
});
}
}