The easiest way to transform collection to array?

后端 未结 8 1793
故里飘歌
故里飘歌 2020-11-27 12:37

Suppose we have a Collection. What is the best (shortest in LoC in current context) way to transform it to Foo[]? Any well-known

8条回答
  •  清酒与你
    2020-11-27 12:53

    Alternative solution to the updated question using Java 8:

    Bar[] result = foos.stream()
        .map(x -> new Bar(x))
        .toArray(size -> new Bar[size]);
    

提交回复
热议问题