Easy way to convert Iterable to Collection

前端 未结 19 2335
忘了有多久
忘了有多久 2020-11-28 01:39

In my application I use 3rd party library (Spring Data for MongoDB to be exact).

Methods of this library return Iterable, while the rest of my

19条回答
  •  广开言路
    2020-11-28 02:31

    IteratorUtils from commons-collections may help (although they don't support generics in the latest stable version 3.2.1):

    @SuppressWarnings("unchecked")
    Collection list = IteratorUtils.toList(iterable.iterator());
    

    Version 4.0 (which is in SNAPSHOT at this moment) supports generics and you can get rid of the @SuppressWarnings.

    Update: Check IterableAsList from Cactoos.

提交回复
热议问题