我经常在很多地方读到应该避免返回 aiterable并改为返回 a collection。例如 -
public Iterable<Maze> Portals() {
// a list of some maze configurations
List<Maze> mazes = createMazes();
...
return Collections.unmodifiableList(mazes);
}
由于返回 aniterable仅对在foreach循环中使用它有用,因此 whilecollection已经提供了 aniterator并提供了更多控制。你能告诉我什么时候专门返回iterable一个方法是有益的吗?或者我们应该总是返回 a collection?
注意:这个问题与 Guava 库无关