Should i return List or ArrayList

后端 未结 4 860
时光取名叫无心
时光取名叫无心 2020-12-31 10:41

I find myself agreeing to return an interface instead of a concrete class.

The reason is simple, i want loose coupling.

But will there be other implication

4条回答
  •  [愿得一人]
    2020-12-31 11:45

    It's best to return the most generic type that's appropriate for your interface.

    If there's some reason why ArrayList is inherently appropriate for the data you're returning then you should use that. Typically List is fine but you might also consider using Collection if the returned values are inherently unordered:

    Diagram of Java collections hierarchy

提交回复
热议问题