• Stream系列(三) FlatMap方法使用


    将多维嵌套列表转换为单维列表

    EmployeeTestCase.java
    package com.example.demo;
    
    import lombok.Data;
    import lombok.ToString;
    import lombok.extern.log4j.Log4j2;
    import org.junit.Test;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.LongStream;
    import java.util.stream.Stream;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertTrue;
    
    @Log4j2
    public class EmployeeTestCase extends BaseTest{
    
        @Test
        public void flatMap() {
            List<Employee> employeesAll = listFlat.stream().flatMap(Collection::stream).collect(Collectors.toList());
            assertTrue(employeesAll.size() == 5);
    
            List<Employee> employeesAll2 = listFlat.stream().flatMap(employees -> {
                Stream<Employee> stream = employees.stream();
                return stream;
            }).collect(Collectors.toList());
            assertEquals(employeesAll, employeesAll2);
    
            List<Long> listFlatLong = listFlat.stream()
                    .flatMap(employees -> employees.stream())
                    .peek(System.out::println)
                    .flatMapToLong(employee -> LongStream.of(employee.getId()))
                    .peek(System.out::println)
                    .boxed()
                    .collect(Collectors.toList());
            log.info("listFlatLong:{}", listFlatLong.toString());
    
            List<Long> listFlatLong2 = listFlat.stream()
                    .flatMap(employees -> employees.stream())
                    .peek(System.out::println)
                    .mapToLong(Employee::getId)
                    .peek(System.out::println)
                    .boxed()
                    .collect(Collectors.toList());
    
            List<Double> listFlatName = listFlat.stream().flatMap(employees -> employees.stream())
                    .peek(System.out::println)
                    .map(employee -> employee.getSalary())
                    .peek(System.out::println)
                    .collect(Collectors.toList());
        }
    
    
    
    }
    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分钟学习

  • 相关阅读:
    话说Hibernate和ADO.NET —练习随笔小记
    二次开发WinWebMail邮件系统接口 企业邮件服务器解决方案
    一个Windows后台服务(.Net的C#版) 定时访问数据库循环发送手机短信
    SQL UPDATE 联合表更新的问题
    2009新的篇章,惠海→时代财富→广佛都市网
    在WebService中使用Session或Cookie实现WebService身份验证(客户端是Flex)
    门户网站的形成—CMS内容管理系统
    CSS实现0.5px的边框或线
    《后人诗》
    CentOS6下docker的安装和使用
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11936136.html
Copyright © 2020-2023  润新知