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 <T> T getFirstNonNull(Collection<T> c) {
    T ret = null;//  w  w  w .j  a v a 2s .c om
    Iterator<T> it = c.iterator();
    while (it.hasNext() && ret == null) {
        T t = it.next();
        if (t != null)
            ret = t;
    }
    return ret;
}

From source file:Main.java

public static <K> Object firstElementOrNull(Iterable iterable) {
    Iterator iterator = iterable.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    } else {//from  w  w w  . ja  v a2  s.c om
        return null;
    }
}

From source file:Main.java

public static boolean hasNotItems(Iterator<?> iters) {
    return (iters != null) && (iters.hasNext());
}

From source file:Main.java

public static <T> List<T> subtract(Collection<T> a, Collection<T> b) {
    ArrayList list = new ArrayList(a);
    Iterator i$ = b.iterator();

    while (i$.hasNext()) {
        Object element = i$.next();
        list.remove(element);/*from  w w w . j a v  a  2 s  . c o  m*/
    }

    return list;
}

From source file:Main.java

/**
 * Add all items in iterator to target collection
 * @param <T>/*  w ww .jav  a 2s  .  com*/
 * @param <U>
 * @param source
 * @param target
 * @return
 */
public static <T, U extends Collection<T>> U addAll(Iterator<T> source, U target) {
    while (source.hasNext()) {
        target.add(source.next());
    }
    return target; // for chaining
}

From source file:Main.java

public static <T> T getSingleElement(Collection<T> collection) {
    Iterator<T> iterator = collection.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}

From source file:Main.java

public static ArrayList<String> getHashMap(HashMap<String, String> hm) {
    ArrayList<String> a = new ArrayList<String>();
    Set s = hm.entrySet();// ww w . j a va 2  s .co m
    Iterator it = s.iterator();
    while (it.hasNext()) {
        Map.Entry m = (Map.Entry) it.next();
        a.add(m.getKey() + "\t" + m.getValue());
    }
    return a;
}

From source file:Main.java

/**
 * Adds all elements in the iteration to the given collection.
 * //from   ww  w  . j  a va2 s  .  co  m
 * @param collection  the collection to add to, must not be null
 * @param iterator  the iterator of elements to add, must not be null
 * @throws NullPointerException if the collection or iterator is null
 */
public static <T> void addAll(Collection<T> collection, Iterator<T> iterator) {
    while (iterator.hasNext()) {
        collection.add(iterator.next());
    }
}

From source file:Main.java

public static String getClasspathString(List<File> c) {
    StringBuilder sb = new StringBuilder();
    Iterator<File> i = c.iterator();
    if (i.hasNext()) {
        sb.append(i.next().getAbsolutePath());
        while (i.hasNext()) {
            sb.append(File.pathSeparatorChar);
            sb.append(i.next().getAbsolutePath());
        }//from ww  w  .  j  a va2  s  .c o  m
    }
    return sb.toString();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String getKeyByValue(Map<?, String> hm, String value) {
    Set<?> s = hm.entrySet();
    Iterator<?> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry m = (Map.Entry) it.next();
        if (m.getValue().equals(value))
            return m.getKey() + "";
    }/*w  w  w. j a va2 s  .c om*/
    return null;
}