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

Stream系列(二)Map方法使用

时间:2019-11-25 11:52:50      阅读:405      评论:0      收藏:0      [点我收藏+]

标签: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)));
        }

 

Stream系列(二)Map方法使用

标签:extern   junit   pack   double   turn   ==   port   final   map   

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

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