我经常在许多地方读过,应该避免返回iterable并返回collection.例如 -
public Iterable<Maze> Portals() {
// a list of some maze configurations
List<Maze> mazes = createMazes();
...
return Collections.unmodifiableList(mazes);
}
Run Code Online (Sandbox Code Playgroud)
因为返回a iterable只对在foreach循环中使用它有用,同时collection已经提供了一个iterator并提供了更多的控制.你能告诉我什么时候特意返回iterable一个方法是有益的吗?或者我们应该总是返回一个collection?
注意:这个问题与Guava库无关