When should I accept a parameter of Iterable vs. Collection in Java?

后端 未结 9 1618
独厮守ぢ
独厮守ぢ 2021-01-30 01:06

What are the considerations of using Iterable vs. Collection in Java?

For example, consider implementing a type that is primarily concerned with contai

9条回答
  •  星月不相逢
    2021-01-30 01:09

    Use the most general interface that you can. Since all you're going to do is iterate, then I would say Iterable is the way to go (since it allows lazy iterators, etc.). You don't care where the iterator is coming from, so don't restrict it more than you have to.

提交回复
热议问题