Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Iterables present two methods for getLast

 public static <T> T getLast(Iterable<T> iterable);
 public static <T> T getLast(Iterable<T> iterable, @Nullable T defaultValue);

but only one for getFirst

 public static <T> T getFirst(Iterable<T> iterable, @Nullable T defaultValue);

Is there are any design/implementation reason for breaking symmetry?

question from:https://stackoverflow.com/questions/8473826/why-there-is-no-getfirstiterable-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
117 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...