在 Google Guava (Java) 中,为什么 Iterables.getFirst() 和 getLast() 不一致?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7832479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Google Guava (Java), why are Iterables.getFirst() and getLast() inconsistent?
提问by kevinarpe
From Google Guava JavaDoc for Iterables
:
来自 Google Guava JavaDoc 的Iterables
:
static <T> T getFirst(Iterable<T> iterable, T defaultValue)
-> Returns the first element in iterable
or defaultValue
if the iterable is empty.
- >返回的第一个元素中iterable
或者defaultValue
如果该迭代是空的。
static <T> T getLast(Iterable<T> iterable)
-> Returns the last element of iterable
.
-> 返回iterable
.
static <T> T getLast(Iterable<T> iterable, T defaultValue)
-> Returns the last element of iterable
or defaultValue
if the iterable is empty.
->如果可迭代对象为空,则返回iterable
or的最后一个元素defaultValue
。
One static method is missing (to me):
缺少一种静态方法(对我而言):
static <T> T getFirst(Iterable<T> iterable)
-> Returns the first element of iterable
.
-> 返回 的第一个元素iterable
。
Do you know the reason for this inconsistency?
你知道这种不一致的原因吗?
采纳答案by Sean Owen
Because it's too simple to justify a helper method. The method would just be iterable.iterator().next()
and would have behavior exactly analogous to getLast()
.
因为证明辅助方法的合理性太简单了。该方法将只是iterable.iterator().next()
并且将具有与 完全类似的行为getLast()
。