Java Collection Element Get getIndexedValue(Object collection, int index)

Here you can find the source of getIndexedValue(Object collection, int index)

Description

get Indexed Value

License

Apache License

Declaration

public static Object getIndexedValue(Object collection, int index) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j  a v  a 2s .co m
 * Copyright 2005-2018 Dozer Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static Object getIndexedValue(Object collection, int index) {
        Object result = null;
        if (collection instanceof Object[]) {
            Object[] x = (Object[]) collection;
            if (index < x.length) {
                return x[index];
            }
        } else if (collection instanceof Collection) {
            Collection<?> x = (Collection<?>) collection;
            if (index < x.size()) {
                Iterator<?> iter = x.iterator();
                for (int i = 0; i < index; i++) {
                    iter.next();
                }
                result = iter.next();
            }
        }
        return result;
    }
}

Related

  1. getFlatString(Collection elements)
  2. getGivenClassObject(Collection coll, Class clazz)
  3. getHighest(final Collection elements, final Comparator c)
  4. getImplement(Class aClass, Collection> classCollection)
  5. getIndex(Object collectionOrArray, int index)