记录一次flatMap使用

记录一次flatMap使用

起因

原有的数据返回格式List里面嵌套一个List 而我要取出嵌套List里面的value
在这里插入图片描述

[DictionarieVO(typeName=staffGroup, voList=[x(key=0, value=高管), DictionarieVO.DictionarieTypeVO(key=1, value=白领), DictionarieVO.DictionarieTypeVO(key=2, value=蓝领), DictionarieVO.DictionarieTypeVO(key=3, value=店端), DictionarieVO.DictionarieTypeVO(key=4, value=顾问), DictionarieVO.DictionarieTypeVO(key=5, value=挂靠)])]

原来的解决方式

List<List<String>> a =  dictionarieVoLsit.getData().stream()
        .map(dictionarieVO -> {

            List<DictionarieVO.DictionarieTypeVO> list = dictionarieVO.getVoList();
            List<String> employeementGroupNameList = list.stream()
                    .map(dictionarieTypeVO -> {
                        String employeementGroupName = dictionarieTypeVO.getValue();
                        return employeementGroupName;
                    })
                    .collect(Collectors.toList());
            return employeementGroupNameList;
        })
        .collect(Collectors.toList());

List<String> b=new ArrayList<>();
for (List<String> list : a) {
    b.addAll(list);
}

但是代码不优雅且有点脱裤子放屁的意思。
后来问了别人,通过flatMap的方式解决了。

RpcResult<List<DictionarieVO>> dictionarieVoLsit = baseClientFeignApi.getDictionaries(Arrays.asList("staffGroup"));
                if (!dictionarieVoLsit.isSuccess()) {
                    log.error("BaseClientFeignApi.getDictionaries() 调用异常:{}", dictionarieVoLsit.getErrorMessage());
                    BizExceptionUtil.throwBusinessException(ErrorCodeEnum.SYS_NOT_FOUND.getCode(), "查询员工列表失败");
                }

                List<String> employeeGroupNameList = dictionarieVoLsit.getData().stream()
                        .map(DictionarieVO::getVoList)
                        .flatMap(Collection::stream)
                        .map(DictionarieVO.DictionarieTypeVO::getValue)
                        .collect(Collectors.toList());

这里可以认为flatMap把很多个流,最后合并成了一条。

flatMap

在这里插入图片描述

例子

例子1

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



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

附录

Java 8 stream流之flatMap
Java常用Api
FlatMap方法使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boy快快长大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值