Example usage for java.util Iterator next

List of usage examples for java.util Iterator next

Introduction

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

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:Main.java

public static Object findObject(Collection<?> c, Object o, Comparator cmp) {
    if (c != null && o != null && cmp != null) {
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            Object current = it.next();
            if (o.getClass().equals(current.getClass())) {
                if (cmp.compare(current, o) == 0) {
                    return current;
                }/*from   w  w  w. j  a va  2s .  c  o m*/
            }
        }
    }
    return null;
}

From source file:Main.java

public static boolean equalsSet(Collection c1, Collection c2) {
    boolean equals;
    if (c1 == null) {
        equals = (c2 == null);/*from   w  ww.  j  a  v  a  2 s . c  o  m*/
    } else if (c2 == null) {
        equals = false;
    } else if (c1.size() == c2.size()) {
        equals = true;
        Iterator iterC1 = c1.iterator();
        while (equals && iterC1.hasNext()) {
            Object o1 = iterC1.next();
            equals = c2.contains(o1);
        }
    } else {
        equals = false;
    }
    return equals;
}

From source file:Main.java

/**
 * Removes duplicate objects from the Collection.
 * @param p_collection a Collection/*from   w w w  .j  a  va  2  s.c  o  m*/
 * @returns true if one or more duplicate objects were removed.
 */
public static boolean removeDuplicates(Collection p_collection) {
    if (p_collection == null) {
        return false;
    }
    HashSet set = new HashSet(p_collection.size());
    Iterator it = p_collection.iterator();
    while (it.hasNext()) {
        set.add(it.next());
    }
    if (set.size() != p_collection.size()) {
        p_collection.clear();
        p_collection.addAll(set);
        return true;
    }
    return false;
}

From source file:Main.java

public static Optional<XMLEvent> getStartElement(List<XMLEvent> events, String elementName) {
    Iterator<XMLEvent> iterator = events.iterator();

    while (iterator.hasNext()) {
        XMLEvent event = iterator.next();

        if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(elementName))
            return Optional.of(event);

    }// w w  w.j a v a  2s. com

    return Optional.empty();
}

From source file:Main.java

/**
 * Build a set from any {@link Iterator}.
 * @param it//  ww  w.ja  v  a 2  s. co m
 * @return
 */
public static <E> Set<E> buildSetFromIterator(final Iterator<E> it) {
    final Set<E> set = new HashSet<>();

    while (it.hasNext()) {
        set.add(it.next());
    }

    return set;
}

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();//www.  jav a  2 s. co  m
        }
    }
}

From source file:Main.java

public static long[] convertListLongs(List<Long> longs) {
    long[] ret = new long[longs.size()];
    Iterator<Long> iterator = longs.iterator();
    for (int i = 0; i < ret.length; i++) {
        ret[i] = iterator.next();
    }//from ww w .  j  a  v  a 2s .c o m
    return ret;
}

From source file:Main.java

public static <T1, T2> void removeHashMapElementByHash(ConcurrentHashMap<T1, T2> target, int hashcode) {
    Iterator<T1> iter = target.keySet().iterator();
    Object key = null;//from  ww  w.j a v a  2 s  .  c  o  m
    while (iter.hasNext()) {
        key = iter.next();
        if (key.hashCode() == hashcode) {
            target.remove(key);
        }
    }
}

From source file:Main.java

public static <T> Collection<? extends T> split(Collection<? extends T> d, int n) {
    Collection<T> tmp = new ArrayList<>();
    Iterator it = d.iterator();
    int k = Math.max(0, n);
    while (it.hasNext() && k > 0) {
        tmp.add((T) it.next());
        k--;//  w  w w  .ja  va2s .  co m
    }
    return tmp;
}

From source file:Main.java

/**
 * @return een int array van alle elementen uit de <tt>collection</tt>.
 *//* www . j a  v a  2 s. c om*/
public static int[] toIntArray(Collection<Integer> collection) {
    int[] res = new int[collection.size()];
    Iterator<Integer> iterator = collection.iterator();
    for (int i = 0; i < collection.size(); i++) {
        res[i] = iterator.next();
    }
    return res;
}