• Stream系列(一)Filter方法使用


    Filter 是过滤器,也可以当查询方法使用

    视频讲解: https://www.bilibili.com/video/av76583051/

    EmployeeTestCase.java

    package com.example.demo;
     
    import lombok.extern.log4j.Log4j2;
    import org.junit.Test;
    import java.util.List;
    import java.util.Optional;
    import java.util.stream.Collectors;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertTrue;
     
    @Log4j2
    public class EmployeeTestCase extends BaseTest{
        @Test
        public void filter() {
            //获取包含Alex员工列表
            List<Employee> employeeIs1 = list.stream().filter(e -> e.getName().contains("Alex")).collect(Collectors.toList());
            assertTrue(null != employeeIs1 && employeeIs1.get(0).getId() == 1l);
            //获取包含Jacks名称的首位员工
            Optional<Employee> employeeJaskFirst2 = list.stream().filter(e -> e.getName().contains("Jack")).findFirst();
            assertEquals(employeeJaskFirst2.get().getId(), 3);
            //获取所有薪酬为1500的员工
            List<Employee> employees1350 = list.stream().filter(employee -> employee.getSalary() == 1500).peek(System.out::println).collect(Collectors.toList());
            assertEquals(2, employees1350.size());
        }
    }

    BaseTest.java
        package com.example.demo;
    
        import java.util.Arrays;
        import java.util.List;
    
        public class BaseTest {
            protected static final List<Employee> list = Arrays.asList(
                    new Employee(1, "Alex", 1000),
                    new Employee(2, "Michael", 2000),
                    new Employee(3, "Jack", 1500),
                    new Employee(4, "Owen", 1500),
                    new Employee(5, "Denny", 2000));
    
            protected static final List<List<Employee>> listFlat = Arrays.asList(
                    Arrays.asList(new Employee(1, "Alex", 1000),
                                  new Employee(2, "Michael", 2000)),
                    Arrays.asList(new Employee(3, "Jack", 1500),
                                  new Employee(4, "Owen", 1500)),
                    Arrays.asList(new Employee(5, "Denny", 2000)));
        }

     关注公众号,坚持每天3分钟学习

  • 相关阅读:
    收藏的 30 个CSS 代码段
    Power designer 15生成sql server 2005/2008列注释
    华为网盘
    敏捷宣言(一)
    敏捷宣言(三) Scrum是一种敏捷实践
    敏捷宣言(二) 何为敏捷
    我也曾哭过[转]
    你以为我穷,不好看,就没有感情吗?[转]
    关于异性朋友
    Gone With the Wind[转]
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11926422.html
Copyright © 2020-2023  润新知