Why there is no getFirst(iterable) method?

后端 未结 2 1378
清歌不尽
清歌不尽 2021-01-03 17:42

Iterables present two methods for getLast

 public static  T getLast(Iterable iterable);
 public static  T getLast(It         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 18:10

    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.

    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).

    Also, there is not a simple way to get the last element, hence getLast(iterable) and getLast(iterable, default)

提交回复
热议问题