码迷,mamicode.com
首页 > 其他好文 > 详细

Stream系列(一)Filter方法使用

时间:2019-11-25 11:15:30      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:获取   str   class   case   名称   out   bsp   employees   amp   

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

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)));
    }

 

Stream系列(一)Filter方法使用

标签:获取   str   class   case   名称   out   bsp   employees   amp   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11926422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!