Iterators { public static <T> Optional<T> getLast(Iterator<T> iterator) { if (!iterator.hasNext()) { return Optional.absent(); } T last; do { last = iterator.next(); } while (iterator.hasNext()); return Optional.of(last); } private  Iterators(); static Optional<T> getFirst(Iterator<T> iterator); static Optional<T> getLast(Iterator<T> iterator);  }
@Test public void getLast() { assertThat(Iterators.getLast(iteratorOf(1)).get(), is(1)); assertThat(Iterators.getLast(iteratorOf(1, 2)).get(), is(2)); }