Java8新特性之stream流的使用

Java8新特性之stream流的使用
1、新建一个实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {

    private static final long serialVersionUID = -6936991705437335757L;

    private Integer id;
    private String name;
    private Integer age;
    private String grade;
    private Integer weight;

}

2、stream流应用案列
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("test")
public class StudentController {
    @PostMapping("test")
    public void test() {
        List<Student> students = Arrays.asList(
                new Student(5, "小高", 15, "98",60),
                new Student(1, "小高", 17, "98",60),
                new Student(3, "小张", 20, "50",40),
                new Student(4, "小胡", 23, "98",30),
                new Student(2, "小叶", 22, "98",50)
        );

        // 1.1 将list转成stream流,并使用filter找出年龄大于18岁的学生
        List<Student> student1 = students.stream().filter(x -> x.getAge() > 18)
                .collect(Collectors.toList());
        System.out.println(student1);

        // 1.2 将list转成stream流,并使用filter找出年龄大于18岁的学生id集合--方法一
        List<Integer> id1 = student1.stream().map(Student::getId).collect(Collectors.toList());
        System.out.println(id1);

        // 1.3 将list转成stream流,并使用filter找出年龄大于18岁的学生id集合--方法二
        List<Integer> id2 = students.stream().filter(x -> x.getAge() > 18).map(Student::getId)
                .collect(Collectors.toList());
        System.out.println(id2);

        // 1.4 根据学生姓名分组
        Map<String, List<Student>> student2 = students.stream().collect(Collectors.groupingBy(Student::getName));
        System.out.println(student2);

        // 1.5 根据学生姓名分组统计人数
        Map<String, Long> studentNum = students.stream().collect(Collectors.groupingBy(Student::getName
                , Collectors.counting()));
        System.out.println(studentNum);

        // 1.6 根据学生姓名和年龄分组
        Map<String, Map<Integer, List<Student>>> student3 = students.stream().collect(Collectors.groupingBy(Student::getName,
                Collectors.groupingBy(Student::getAge)));
        System.out.println(student3);

        // 1.7 根据学生姓名和年龄去重
        List<Student> userRoles = students.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getName()
                                + o.getAge()))), ArrayList::new));
        System.out.println(userRoles);

        // 1.8 根据student整体去重
        List<Student> students4 = students.stream().distinct().collect(Collectors.toList());
        System.out.println(students4);

        // 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()));
        System.out.println(map);

        // 2.0 foreach
        students.forEach(y -> {
            Map<String, Object> result = new HashMap<>();
            // 根据需求做相关逻辑判断
        });

        // 根据weight字段排序--正序
        List<Student> students5 = students.stream().sorted(Comparator.comparing(Student::getWeight)).collect(Collectors.toList());
        System.out.println(students5);

        // 根据weight字段排序-倒序
        List<Student> students6 = students.stream().sorted(Comparator.comparing(Student::getWeight).reversed()).collect(Collectors.toList());
        System.out.println(students6);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值