The easiest way to transform collection to array?

后端 未结 8 1823
故里飘歌
故里飘歌 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:55

    Here's the final solution for the case in update section (with the help of Google Collections):

    Collections2.transform (fooCollection, new Function() {
        public Bar apply (Foo foo) {
            return new Bar (foo);
        }
    }).toArray (new Bar[fooCollection.size()]);
    

    But, the key approach here was mentioned in the doublep's answer (I forgot for toArray method).

提交回复
热议问题