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)
| 824 | |
| 825 | |
| 826 | public static <T> T getLast(Iterable<T> iterable) { |
| 827 | // TODO(kevinb): Support a concurrently modified collection? |
| 828 | if (iterable instanceof List) { |
| 829 | List<T> list = (List<T>) iterable; |
| 830 | if (list.isEmpty()) { |
| 831 | throw new NoSuchElementException(); |
| 832 | } |
| 833 | return getLastInNonemptyList(list); |
| 834 | } |
| 835 | return Iterators.getLast(iterable.iterator()); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Returns the last element of {@code iterable} or {@code defaultValue} if |
no test coverage detected