起因
原有的数据返回格式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)));
}