Java Collection Remove removeElement(final int index, final Collection coll)

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

Description

makes sense only if iteration order deterministic!

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*//from   ww  w  . j a v  a2  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 removeElement(final int index, final Collection<T> coll) {
        return getElement(true, index, coll);
    }

    /**
     * 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. removeDuplicates(Collection original)
  2. removeDuplicates(Collection p_collection)
  3. removeDuplicates(Collection objects)
  4. removeDuplicates(Collection elements)
  5. removeElement(Collection coll)
  6. removeElementsOfList(Collection set, Collection elementsToRemove)
  7. removeIfNotPresent(Collection collection, Collection permitted)
  8. removeOutStrings(Collection values, String newString)
  9. removeRightSet(Collection left, Collection right)