为什么没有getFirst(iterable)方法? [英] Why there is no getFirst(iterable) method?
问题描述
迭代提供两种方法 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);
是否有任何破坏对称性的设计/实现原因?
Is there are any design/implementation reason for breaking symmetry?
推荐答案
我认为关键是没有理由让 getFirst(iterable)
,因为这可以通过 iterable.iterator()。next()
来完成。 Guava做了一个很好的尝试,保持API小,所以不添加可以/应该轻松以另一种方式做的事情。
I think the point is that there is no reason for a getFirst(iterable)
in that this could be done with iterable.iterator().next()
. Guava makes an excellent attempt to keep the API small and so does not add things that could / should be done easily another way.
另一方面,还没有一种机制来测试一个iterable是否为空,如果是这样,返回一个默认值,而不是第一个值。因此, getFirst(iterable,default)
。
On the other hand, there is not already a mechanism to test if an iterable is empty and if so return a default value instead of the first value. Hence, getFirst(iterable, default)
.
另外,没有一个简单的方法来获取最后一个元素,因此 getLast(iterable)
和 getLast(iterable,default)
Also, there is not a simple way to get the last element, hence getLast(iterable)
and getLast(iterable, default)
这篇关于为什么没有getFirst(iterable)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!