Returns the last element of iterable. If iterable is a List with RandomAccess support, then this operation is guaranteed to be O(1). @return the last element of iterable @throws NoSuchElementException if the iterable is empty
(Iterable<T> iterable)
| 762 | * @throws NoSuchElementException if the iterable is empty |
| 763 | */ |
| 764 | public static <T> T getLast(Iterable<T> iterable) { |
| 765 | // TODO(kevinb): Support a concurrently modified collection? |
| 766 | if (iterable instanceof List) { |
| 767 | List<T> list = (List<T>) iterable; |
| 768 | if (list.isEmpty()) { |
| 769 | throw new NoSuchElementException(); |
| 770 | } |
| 771 | return getLastInNonemptyList(list); |
| 772 | } |
| 773 | |
| 774 | return Iterators.getLast(iterable.iterator()); |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Returns the last element of {@code iterable} or {@code defaultValue} if |
no test coverage detected