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

后端 未结 9 1538
独厮守ぢ
独厮守ぢ 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:20

    If you go for Collection, then your class can be initialised from a collection only, if you go for Iterable, then you can initialise from either collection or iterable.

    As the effort and performance for both is going to be the same, it makes total sense to accept Iterable in the constructor.

提交回复
热议问题