标签:extern junit pack double turn == port final map
Stream 将List 里面的对象转换为新的对象
EmployeeTestCase.java
package com.example.demo; import lombok.Data; import lombok.ToString; import lombok.extern.log4j.Log4j2; import org.junit.Test; import java.util.List; import java.util.stream.Collectors; @Log4j2 public class EmployeeTestCase extends BaseTest{ @Test public void map(){
//将Employee 转换 Leader 列表 List<Leader> leaders = list.stream().filter(employee -> employee.getSalary() == 2000).map(employee -> { Leader leader = new Leader(); leader.setName(employee.getName()); leader.setSalary(employee.getSalary()); return leader; }).peek(System.out::println).collect(Collectors.toList()); } @Data @ToString class Leader{ private String name; private double salary; } }
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))); }
标签:extern junit pack double turn == port final map
原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11926445.html