为什么没有 getFirst(iterable) 方法? [英] Why there is no getFirst(iterable) method?

查看:24
本文介绍了为什么没有 getFirst(iterable) 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迭代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.

另一方面,还没有一种机制来测试可迭代对象是否为空,如果是,则返回默认值而不是第一个值.因此,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆