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

public static boolean processReadySet(Set readySet) throws Exception {
    Iterator iterator = readySet.iterator();
    while (iterator.hasNext()) {
        SelectionKey key = (SelectionKey) iterator.next();
        iterator.remove();
        if (key.isConnectable()) {
            boolean connected = processConnect(key);
            if (!connected) {
                return true; // Exit
            }//w  w  w .  j  a va2  s .  c  o  m
        }
        if (key.isReadable()) {
            String msg = processRead(key);
            System.out.println("[Server]: " + msg);
        }
        if (key.isWritable()) {
            System.out.print("Please enter a message(Bye to quit):");
            String msg = userInputReader.readLine();

            if (msg.equalsIgnoreCase("bye")) {
                return true; // Exit
            }
            SocketChannel sChannel = (SocketChannel) key.channel();
            ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
            sChannel.write(buffer);
        }
    }
    return false; // Not done yet
}

From source file:Main.java

public static boolean remove(Collection collection, Object object) {
    boolean removed = false;
    Iterator it = collection.iterator();
    while (it.hasNext()) {
        Object o = it.next();//from  w ww  . j  a  v a2s  . com
        if (o.equals(object)) {
            removed = true;
            it.remove();
            break;
        }
    }

    return removed;
}

From source file:Main.java

public static <T> void resize(List<T> list, int size, Class<T> clazz) {
    if (size == list.size()) {
        return;//from   w ww  .  j a va 2s  . c  om
    }
    if (size < list.size()) {
        Iterator<T> iter = list.iterator();
        for (int i = 1; iter.hasNext(); i++) {
            iter.next();
            if (i > size) {
                iter.remove();
            }
        }
    } else {
        for (int i = 0; i <= size - list.size(); i++) {
            try {
                list.add(clazz != null ? clazz.newInstance() : null);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

From source file:Main.java

public static Collection<Object> filterCollection(Collection<Object> src, Collection<Object> dest) {

    if (isEmpty(dest))
        return src;
    Iterator<Object> iterator = src.iterator();
    while (iterator.hasNext()) {

        Object next = iterator.next();
        if (dest.contains(next)) {

            iterator.remove();
        }//from www  .  j a v  a2 s  .c o m
    }

    return src;
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.cleanDL.java

static void handleDoc(List<DocMention> mdocs) {
    List<DocMention> toRemove = Lists.newArrayList();
    for (DocMention m1 : mdocs) {
        for (DocMention m2 : mdocs) {
            if (m1.equals(m2))
                continue;
            if (m1.mention.contains(m2.mention) && m1.start <= m2.start && m1.end >= m2.end) {
                System.out.println("removing substring mention " + m2.mention + "," + m2.start + "," + m2.end
                        + " for " + m1.mention + "," + m1.start + "," + m1.end);
                toRemove.add(m2);//  w w  w  .j a va2s  . c o  m
            }
            if (m2.mention.contains(m1.mention) && m2.start <= m1.start && m2.end >= m1.end) {
                System.out.println("removing substring mention " + m1.mention + " for " + m2.mention);
                toRemove.add(m1);
            }
        }
    }
    Iterator<DocMention> it = mdocs.iterator();
    while (it.hasNext()) {
        DocMention m = it.next();
        if (toRemove.contains(m))
            it.remove();
    }
}

From source file:Main.java

public static Map filterNullValues(Map map) {
    Iterator<Map.Entry> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry next = iterator.next();
        if (next.getValue() == null) {
            iterator.remove();
        }//  ww w .j  a  va2  s.c o m
    }
    return map;
}

From source file:Main.java

/**
 * <p>Generate reverse result</p>
 * @param allResult all result//from   www .ja  va  2 s  .co  m
 * @param partResult part result
 * @return reverse result
 */
public static <T extends Object> List<T> getReverseResult(Set<T> allResult, Set<T> partResult) {
    List<T> resultList = new ArrayList<T>();
    Map<T, T> map = new HashMap<T, T>();
    if (partResult != null) {
        for (T obj : partResult) {
            map.put(obj, obj);
        }
    }
    if (allResult != null) {
        Iterator<T> itor = allResult.iterator();
        while (itor.hasNext()) {
            T obj = itor.next();
            if (map.containsKey(obj)) {
                itor.remove();
            } else {
                resultList.add(obj);
            }
        }
    }
    return resultList;
}

From source file:Main.java

public static void removeItemFromCollectionByUsingIteratorRemoveMethod(Collection<String> collection,
        String elementToBeRemoved) {

    Iterator iterator = collection.iterator();

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

        if (element.equals(elementToBeRemoved)) {
            iterator.remove();
        }/*from ww w  .  j  a  v  a 2  s  .c o  m*/
    }
}

From source file:Main.java

/**
 * Filter the collection by applying a Predicate to each element. If the
 * predicate returns false, remove the element. If the input collection or
 * predicate is null, there is no change made.
 *
 * @param input/*from  w ww .j  a  va 2  s .  c  o  m*/
 * @param predicate
 * @param <V>
 * @return
 */
public static <V> void filter(Collection<V> input, Predicate<V> predicate) {

    Iterator<V> iterator = input.iterator();

    while (iterator.hasNext()) {

        V value = iterator.next();

        if (!predicate.test(value)) {
            iterator.remove();
        }

    }

}

From source file:Main.java

public static ArrayList<Integer> filterPoint(ArrayList<Integer> timePoints) {
    int lastPeriod = -1;
    logInConsole(timePoints);//from ww  w.jav a  2 s.co m
    Iterator<Integer> it = timePoints.iterator();
    while (it.hasNext()) {
        int tmp = it.next();
        if (lastPeriod != -1 && tmp - lastPeriod < MIN_PERIOD) {
            it.remove();
        } else {
            lastPeriod = tmp;
        }
    }
    logInConsole(timePoints);
    return timePoints;
}