Example usage for java.util Iterator hasNext

List of usage examples for java.util Iterator hasNext

Introduction

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

Prototype

boolean hasNext();

Source Link

Document

Returns true if the iteration has more elements.

Usage

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  ww  .ja v a2 s .  c om*/

    return Optional.empty();
}

From source file:Main.java

public static <T> T getSingleIfExist(Iterable<T> iterable) {
    if (iterable == null)
        return null;
    final Iterator<? extends T> iter = iterable.iterator();
    if (!iter.hasNext())
        return null;
    final T result = iter.next();
    if (iter.hasNext()) {
        throw new IllegalArgumentException("More than (expected) one element in " + iterable);
    }//ww  w  .  j av  a  2  s  .c om
    return result;
}

From source file:Main.java

public static <T> Map<T, Integer> getCardinalityMap(final Collection<T> coll) {
    if (coll == null) {
        return null;
    }//from   w  ww .  j a v  a 2s  . c  o  m

    Map<T, Integer> result = new HashMap<T, Integer>();
    Iterator<T> it = coll.iterator();
    while (it.hasNext()) {
        T t = it.next();
        Integer count = result.get(t);
        if (count == null) {
            result.put(t, ONE);
        } else {
            result.put(t, new Integer(count.intValue() + 1));
        }
    }
    return result;
}

From source file:Main.java

public static <E> List<E> iteratorToList(Iterator<E> iterator) {
    List<E> list = new ArrayList<E>();
    while (iterator.hasNext()) {
        list.add(iterator.next());//from ww  w  . ja va  2 s.c om
    }
    return list;
}

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;
                }//ww w  .ja  v  a 2s . co  m
            }
        }
    }
    return null;
}

From source file:Main.java

public static <K> boolean isEquals(Collection<K> collection1, Collection<K> collection2) {

    if (collection1 == null && collection2 == null) {
        return true;
    }//from w w  w  .  ja  v  a2s .c om
    if (collection1 == null || collection2 == null || collection1.size() != collection2.size()) {
        return false;
    }
    boolean result = true;
    Iterator<K> ite2 = collection2.iterator();
    while (ite2.hasNext()) {
        if (!collection1.contains(ite2.next())) {
            result = false;
            break;
        }
    }
    return result;
}

From source file:Main.java

public static final <K, V> void printM(Map<K, V> map, String sep) {
    Iterator<Entry<K, V>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Entry<K, V> e = it.next();
        System.out.append(e.getKey().toString()).append("::").append(e.getValue().toString());
        if (it.hasNext()) {
            System.out.append(sep);
        }/*  w  ww .  j av a 2  s .  co m*/
    }

}

From source file:Main.java

public static int GetIndexFromCollectionUsingID(ArrayList list, String value) {
    Iterator Item = list.iterator();
    int index = -1;
    while (Item.hasNext()) {
        index++;// w w  w. j a  v  a  2  s.com
        if (Item.next().toString().contains(value + " :"))
            return index;

    }
    return -1;
}

From source file:Main.java

public static int GetIndexFromCollection(ArrayList list, String value) {
    Iterator Item = list.iterator();
    int index = -1;
    while (Item.hasNext()) {
        index++;//w  w w. jav  a2 s .  c o  m
        if (Item.next().toString().equalsIgnoreCase(value))
            return index;

    }
    return -1;
}

From source file:Main.java

public static <T> Optional<T> findAnyIn(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return Optional.of(iterator.next());
    }//from  w  w  w.  java 2  s  .  co m
    return Optional.empty();
}