简体   繁体   中英

Convert List of Stream to List

I have a lambda expression where I want to create a list of object but I am getting the error

List<ClassObject> classObject = obj1.getFileId().stream()
                .map(x -> obj1.getCustomObj().stream()
                        .map(y -> obj1.builder().Id(x).name(y.getName()).value(y.getValue())
                                .details(obj1.getDetails()).build()))
                .collect(Collectors.toList());

Error

Type mismatch: cannot convert from List<Stream<ClassObject>> to List<ClassObject>

I want List<ClassObject> as return type from the lambda expression. How can I achieve that?

Use flatMap:

List<ClassObject> classObject = obj1.getFileId().stream()
            .flatMap(x -> obj1.getCustomObj().stream()
                    .map(y -> obj1.builder().Id(x).name(y.getName()).value(y.getValue())
                            .details(obj1.getDetails()).build()))
            .collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM