Example usage for java.util Iterator remove

List of usage examples for java.util Iterator remove

Introduction

In this page you can find the example usage for java.util Iterator remove.

Prototype

default void remove() 

Source Link

Document

Removes from the underlying collection the last element returned by this iterator (optional operation).

Usage

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 *///w  w  w . j a va2 s.  c om
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;
}

From source file:Main.java

public static void trimCollection(Collection collection, int numberOfElements) {
    if (collection.size() < numberOfElements) {
        return;// w w w . j  a  v  a2 s. co  m
    }

    numberOfElements = collection.size() - numberOfElements;
    Iterator it = collection.iterator();
    int counter = 0;

    while (it.hasNext()) {
        if (counter <= numberOfElements) {
            it.next();
            counter++;
        }
        it.remove();
    }
}

From source file:Main.java

/**
 * Searches for and returns the first string which starts with the given
 * prefix. Removes the match from the collection.
 *
 * @param collection the collection./*from   w  ww.jav  a  2s.com*/
 * @param prefix     the string prefix.
 * @return a string, or null if no matches.
 */
public static String popStartsWith(Collection<String> collection, String prefix) {
    Iterator<String> iterator = collection.iterator();

    while (iterator.hasNext()) {
        String element = iterator.next();

        if (element != null && element.startsWith(prefix)) {
            iterator.remove();
            return element;
        }
    }

    return null;
}

From source file:Main.java

private static void frequenceyCount(String input) {
    Map<Character, Integer> hashCount = new HashMap<>();
    Character c;/*from w  ww .  ja  v  a 2  s .c  om*/
    for (int i = 0; i < input.length(); i++) {
        c = input.charAt(i);
        if (hashCount.get(c) != null) {
            hashCount.put(c, hashCount.get(c) + 1);
        } else {
            hashCount.put(c, 1);
        }
    }
    Iterator it = hashCount.entrySet().iterator();
    System.out.println("char : frequency");
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry) it.next();
        System.out.println(pairs.getKey() + " : " + pairs.getValue());
        it.remove();
    }
}

From source file:Main.java

public static void trimMap(Map map, int numberOfElements) {
    if (map.size() < numberOfElements) {
        return;//from   ww  w . jav a2s  .co  m
    }

    numberOfElements = map.size() - numberOfElements;
    Iterator it = map.entrySet().iterator();
    int counter = 0;

    while (it.hasNext()) {
        if (counter <= numberOfElements) {
            it.next();
            counter++;
        }
        it.remove();
    }
}

From source file:Main.java

/**
 * Removes null objects from the Collection.
 * @param p_collection a Collection//from  ww  w. j a  v  a  2s  .c  o  m
 * @returns true if one or more null objects were removed.
 */
public static boolean removeNulls(Collection p_collection) {
    if (p_collection == null) {
        return false;
    }
    boolean removed = false;
    Iterator it = p_collection.iterator();
    while (it.hasNext()) {
        Object o = it.next();
        if (o == null) {
            removed = true;
            it.remove();
        }
    }
    return removed;
}

From source file:Main.java

/**
 * Modify the specified Collection so that only the first <code>limit</code> elements
 * remain, as determined by iteration order. If the Collection is smaller than limit,
 * it is unmodified.//  w  w  w  .j av a 2 s . c o  m
 */
public static void limit(Collection<?> col, int limit) {
    int size = col.size();
    if (size > limit) {
        if (col instanceof List<?>) {
            ((List<?>) col).subList(limit, size).clear();

        } else {
            Iterator<?> itr = col.iterator();
            for (int ii = 0; ii < limit; ii++) {
                itr.next();
            }
            while (itr.hasNext()) {
                itr.next();
                itr.remove();
            }
        }
    }
}

From source file:com.facebook.model.JsonUtil.java

static void jsonObjectClear(JSONObject jsonObject) {
    @SuppressWarnings("unchecked")
    Iterator<String> keys = jsonObject.keys();
    while (keys.hasNext()) {
        keys.next();//  ww w. j a v a2 s  .co m
        keys.remove();
    }
}

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 */// www .  jav  a  2  s  . c o m
private static <K, V> Map.Entry<K, V> getRandomElement(final boolean remove, final Random random,
        final Map<K, V> map) {
    if (map.isEmpty())
        throw new IllegalArgumentException("map is empty!");
    final int index = random.nextInt(map.size());

    final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
    int i = 0;

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

    final Map.Entry<K, V> elem = it.next();
    if (remove)
        it.remove();
    return elem;
}

From source file:Main.java

/**
 * Removes and 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 remove method. Otherwise, it walks the {@code Iterable} to
 * get to the element and remove it. This only works on {@code Iterable}s
 * that are {@code List}s or whose {@code Iterator} implements the optional
 * {@code remove} method./*w  ww  .ja  va  2  s .co  m*/
 *
 * @param <DataType>
 *      The type of data.
 * @param iterable
 *      The iterable to remove the value from.
 * @param index
 *      The 0-based index to remove from the iterable.
 * @return
 *      The value removed from the given index 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.
 * @throws UnsupportedOperationException
 *      If the iterable does not support remove.
 */
public static <DataType> DataType removeElement(final Iterable<DataType> iterable, int index) {
    if (iterable instanceof List<?>) {
        return ((List<DataType>) iterable).remove(index);
    } else {
        if (index < 0) {
            // Bad index.
            throw new IndexOutOfBoundsException("index must be >= 0");
        }

        Iterator<DataType> iterator = iterable.iterator();

        while (iterator.hasNext()) {
            DataType value = iterator.next();

            if (index == 0) {
                iterator.remove();
                return value;
            }

            index--;
        }

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

}