Google Guava “zip” two lists

前端 未结 6 2031
萌比男神i
萌比男神i 2020-12-03 13:34

Using Google Guava (Google Commons), is there a way to merge two equally sized lists into one list, with the new list containing composite objects of the two input lists?

6条回答
  •  没有蜡笔的小新
    2020-12-03 14:27

    As of Guava 21, this is possible via Streams.zip():

    List persons = Streams.zip(names.stream(), ages.stream(), Person::new)
                                  .collect(Collectors.toList());
    

提交回复
热议问题