Easy way to convert Iterable to Collection

前端 未结 19 2273
忘了有多久
忘了有多久 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:30

    As soon as you call contains, containsAll, equals, hashCode, remove, retainAll, size or toArray, you'd have to traverse the elements anyway.

    If you're occasionally only calling methods such as isEmpty or clear I suppose you'd be better of by creating the collection lazily. You could for instance have a backing ArrayList for storing previously iterated elements.

    I don't know of any such class in any library, but it should be a fairly simple exercise to write up.

提交回复
热议问题