Combine two lists of same size (and different type) into list of domain objects using java streams

前端 未结 2 1415
我在风中等你
我在风中等你 2021-01-18 18:56

I\'m having two lists of same size ids and results and I want to create new list with domain objects.

List ids = ...

Lis         


        
相关标签:
2条回答
  • 2021-01-18 19:56

    The equivalent of this way with Streams would be :

    List<DomainObject> list = IntStream.range(0, ids.size())
                                .mapToObj(i -> new DomainObject(ids.get(i), results.get(i))) 
                                .collect(Collectors.toList());
    

    Or take a look at Iterate two Java-8-Streams

    0 讨论(0)
  • 2021-01-18 19:57

    I've found a way to do it using guava zip operator.

    List<DomainObject> list = Streams.zip(ids.stream(), results.stream(), DomainObject::new)
                                .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题