Java Collection Element Get getElement(final int index, final Collection coll)

Here you can find the source of getElement(final int index, final Collection coll)

Description

makes sense only if iteration order deterministic!

License

Open Source License

Declaration

public static <T> T getElement(final int index, final Collection<T> coll) 

Method Source Code

//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*//from   ww w. j  a  va  2 s.c o  m
 * CollectionUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2013 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * makes sense only if iteration order deterministic!
     */
    public static <T> T getElement(final int index, final Collection<T> coll) {
        return getElement(false, index, coll);
    }

    /**
     * makes sense only if iteration order deterministic!
     */
    private static <T> T getElement(final boolean remove, final int index, final Collection<T> coll) {
        if (index >= coll.size())
            throw new IndexOutOfBoundsException(index + " >= " + coll.size());

        final Iterator<T> it = coll.iterator();
        int i = 0;

        while (i++ < index)
            it.next();

        final T elem = it.next();
        if (remove)
            it.remove();
        return elem;
    }
}

Related

  1. getCommon(Collection c1, Collection c2)
  2. getCommonElements( Collection from, Collection to)
  3. getCommonSuffix(Collection c)
  4. getComponentName(Collection existingNames, String name)
  5. getDocIdString(Collection docIds)
  6. getElementClass(Collection collection)
  7. getElementFromSize1(Collection collection)
  8. getEntropy(Collection values)
  9. getEnumNames( Collection values)