@Override @Nullable public Entry<K, V> firstEntry() { return Iterators.getNext(entryIterator(), null); }
@Override @Nullable public Entry<K, V> lastEntry() { return Iterators.getNext(descendingEntryIterator(), null); }
/** * Returns the first element in {@code iterable} or {@code defaultValue} if the iterable is empty. * The {@link Iterators} analog to this method is {@link Iterators#getNext}. * * <p>If no default value is desired (and the caller instead wants a {@link * NoSuchElementException} to be thrown), it is recommended that {@code * iterable.iterator().next()} is used instead. * * <p>To get the only element in a single-element {@code Iterable}, consider using {@link * #getOnlyElement(Iterable)} or {@link #getOnlyElement(Iterable, Object)} instead. * * <p><b>{@code Stream} equivalent:</b> {@code stream.findFirst().orElse(defaultValue)} * * @param defaultValue the default value to return if the iterable is empty * @return the first element of {@code iterable} or the default value * @since 7.0 */ @Nullable public static <T> T getFirst(Iterable<? extends T> iterable, @Nullable T defaultValue) { return Iterators.getNext(iterable.iterator(), defaultValue); }
/** * A sensible definition of {@link #higher} in terms of the {@code iterator} method of * {@link #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may * wish to override {@link #higher} to forward to this implementation. */ protected E standardHigher(E e) { return Iterators.getNext(tailSet(e, false).iterator(), null); }
/** * A sensible definition of {@link #ceiling} in terms of the {@code iterator} method of * {@link #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may * wish to override {@link #ceiling} to forward to this implementation. */ protected E standardCeiling(E e) { return Iterators.getNext(tailSet(e, true).iterator(), null); }
/** * A sensible definition of {@link #lower} in terms of the {@code descendingIterator} method of * {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may * wish to override {@link #lower} to forward to this implementation. */ protected E standardLower(E e) { return Iterators.getNext(headSet(e, false).descendingIterator(), null); }
/** * A sensible definition of {@link #floor} in terms of the {@code descendingIterator} method of * {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may * wish to override {@link #floor} to forward to this implementation. */ protected E standardFloor(E e) { return Iterators.getNext(headSet(e, true).descendingIterator(), null); }
/** * Advances {@code iterator} {@code position + 1} times, returning the * element at the {@code position}th position or {@code defaultValue} * otherwise. * * @param position position of the element to return * @param defaultValue the default value to return if the iterator is empty * or if {@code position} is greater than the number of elements * remaining in {@code iterator} * @return the element at the specified position in {@code iterator} or * {@code defaultValue} if {@code iterator} produces fewer than * {@code position + 1} elements. * @throws IndexOutOfBoundsException if {@code position} is negative * @since 4.0 */ @Nullable public static <T> T get(Iterator<? extends T> iterator, int position, @Nullable T defaultValue) { checkNonnegative(position); advance(iterator, position); return getNext(iterator, defaultValue); }
/** * @since 12.0 */ @GwtIncompatible // NavigableSet @Override public E lower(E e) { return Iterators.getNext(headSet(e, false).descendingIterator(), null); }
/** * Returns the element at the specified position in an iterable or a default * value otherwise. * * <p><b>{@code Stream} equivalent:</b> * {@code stream.skip(position).findFirst().orElse(defaultValue)} * (returns the default value if the index is out of bounds) * * @param position position of the element to return * @param defaultValue the default value to return if {@code position} is * greater than or equal to the size of the iterable * @return the element at the specified position in {@code iterable} or * {@code defaultValue} if {@code iterable} contains fewer than * {@code position + 1} elements. * @throws IndexOutOfBoundsException if {@code position} is negative * @since 4.0 */ @Nullable public static <T> T get(Iterable<? extends T> iterable, int position, @Nullable T defaultValue) { checkNotNull(iterable); Iterators.checkNonnegative(position); if (iterable instanceof List) { List<? extends T> list = Lists.cast(iterable); return (position < list.size()) ? list.get(position) : defaultValue; } else { Iterator<? extends T> iterator = iterable.iterator(); Iterators.advance(iterator, position); return Iterators.getNext(iterator, defaultValue); } }
/** * @since 12.0 */ @GwtIncompatible // NavigableSet @Override public E floor(E e) { return Iterators.getNext(headSet(e, true).descendingIterator(), null); }