【发布时间】:2012-01-18 10:02:00
【问题描述】:
Iterables 为getLast 提供两种方法
public static <T> T getLast(Iterable<T> iterable);
public static <T> T getLast(Iterable<T> iterable, @Nullable T defaultValue);
但getFirst只有一个
public static <T> T getFirst(Iterable<T> iterable, @Nullable T defaultValue);
是否存在破坏对称性的任何设计/实施原因?
【问题讨论】:
-
也许你可以使用Iterables.getOnlyElement
-
@FabianZeindl -
getOnlyElement抛出IllegalArgumentException如果有多个元素。getLast不应该做这种事。 -
这就是为什么我的意思是“也许”-> 在某些情况下。
标签: java collections guava api-design iterable