Search code examples
javaguava

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


From Google Guava JavaDoc for Iterables:

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?


Solution

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