kevinarpe kevinarpe - 3 years ago 103
Java Question

In Google Guava (Java), why are Iterables.getFirst() and getLast() inconsistent?

From Google Guava JavaDoc for

:

static <T> T getFirst(Iterable<T> iterable, T defaultValue)


-> Returns the first element in
iterable
or
defaultValue
if the iterable is empty.

static <T> T getLast(Iterable<T> iterable)


-> Returns the last element of
iterable
.

static <T> T getLast(Iterable<T> iterable, T defaultValue)


-> Returns the last element of
iterable
or
defaultValue
if the iterable is empty.

One static method is missing (to me):

static <T> T getFirst(Iterable<T> iterable)


-> Returns the first element of
iterable
.

Do you know the reason for this inconsistency?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download