Java Iterable getElement(final Iterable iterable, int index)

Here you can find the source of getElement(final Iterable iterable, int index)

Description

Returns the indexed value into the Iterable .

License

Open Source License

Parameter

Parameter Description
DataType The type of data.
iterable The iterable to pull the value from.
index The 0-based index to pull from the iterable.

Exception

Parameter Description
IndexOutOfBoundsException If the index is less than zero orgreater than or equal to the number of elements in the iterable.

Return

The value at the given spot in the iterable.

Declaration

public static <DataType> DataType getElement(final Iterable<DataType> iterable, int index) 

Method Source Code

//package com.java2s;
/*//from  ww w  .j  a v  a  2 s.  c o m
 * File:                CollectionUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright March 25, 2008, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 * See CopyrightHistory.txt for complete details.
 *
 */

import java.util.List;

public class Main {
    /**
     * Returns the indexed value into the {@code Iterable}. It first checks to
     * see if the {@code Iterable} is a {@code List}, and if so calls the get
     * method. Otherwise, it walks the {@code Iterable} to get to the element.
     *
     * @param <DataType> The type of data.
     * @param iterable The iterable to pull the value from.
     * @param index The 0-based index to pull from the iterable.
     * @return The value at the given spot in the iterable.
     * @throws IndexOutOfBoundsException If the index is less than zero or
     * greater than or equal to the number of elements in the iterable.
     */
    public static <DataType> DataType getElement(final Iterable<DataType> iterable, int index) {
        if (iterable instanceof List<?>) {
            return ((List<DataType>) iterable).get(index);
        } else {
            if (index < 0) {
                // Bad index.
                throw new IndexOutOfBoundsException("index must be >= 0");
            }

            for (DataType v : iterable) {
                if (index == 0) {
                    return v;
                }

                index--;
            }

            // Bad index.
            throw new IndexOutOfBoundsException("index >= iterable size");
        }

    }
}

Related

  1. equal(Iterable it1, Iterable it2)
  2. equalsIterablesInOrder(Iterable i1, Iterable i2)
  3. fillFromIterable(C c, Iterable i)
  4. getAny(Iterable iterable)
  5. getByIndex(Iterable iterable, int index)
  6. getIdenticalElement(Iterable iterable)
  7. getLongestCommonToken(Iterable iterable, char tokenSeparatorChar)
  8. getMessageString(Iterable msgs)
  9. getUnique(Iterable iterable)