/** * Shortcut for {@code get(iterator, 0)}. * <p> * Returns the <code>first</code> value in {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * </p> * <p> * The Iterator is advanced to <code>0</code> (or to the end, if * <code>0</code> exceeds the number of entries) as a side effect of this method. * </p> * @param <E> the type of object in the {@link Iterator} * @param iterator the iterator to get a value from * @return the first object * @throws IndexOutOfBoundsException if the request is invalid * @since 4.2 */ public static <E> E first(final Iterator<E> iterator) { return get(iterator, 0); }
/** * Returns the <code>index</code>-th value in {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * <p> * The Iterator is advanced to <code>index</code> (or to the end, if * <code>index</code> exceeds the number of entries) as a side effect of this method. * * @param iterator the iterator to get a value from * @param index the index to get * @param <T> the type of object in the {@link Iterator} * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid * @throws IllegalArgumentException if the object type is invalid * @deprecated since 4.1, use {@code IteratorUtils.get(Iterator, int)} instead */ @Deprecated public static <T> T get(final Iterator<T> iterator, final int index) { return IteratorUtils.get(iterator, index); }
/** * Returns the <code>index</code>-th value in the <code>iterable</code>'s {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * <p> * If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}. * * @param <T> the type of object in the {@link Iterable}. * @param iterable the {@link Iterable} to get a value from, may be null * @param index the index to get * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid */ public static <T> T get(final Iterable<T> iterable, final int index) { CollectionUtils.checkIndexBounds(index); if (iterable instanceof List<?>) { return ((List<T>) iterable).get(index); } return IteratorUtils.get(emptyIteratorIfNull(iterable), index); }
final Map<?, ?> map = (Map<?, ?>) object; final Iterator<?> iterator = map.entrySet().iterator(); return IteratorUtils.get(iterator, i); } else if (object instanceof Object[]) { return ((Object[]) object)[i]; } else if (object instanceof Iterator<?>) { final Iterator<?> it = (Iterator<?>) object; return IteratorUtils.get(it, i); } else if (object instanceof Iterable<?>) { final Iterable<?> iterable = (Iterable<?>) object;