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 String getPrettyString(Collection<?> coll) {
    StringBuffer sb = new StringBuffer();
    Iterator<?> it = coll.iterator();

    while (it.hasNext()) {
        String s = it.next().toString();
        sb.append(s + "\n");
    }//from ww  w.j a v a  2  s .c  om

    return sb.toString().trim();
}

From source file:Main.java

public static void showList(List<? extends Object> lst) {
    Iterator<? extends Object> it = lst.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }//from w  w  w  .  j a  v a2 s. c  o m
}

From source file:Main.java

public static <T> Object getFirst(Iterable<T> c) {
    final Iterator<T> it = c.iterator();
    if (!it.hasNext()) {
        return null;
    } else {/*from w ww .  ja  v  a2  s  .  co m*/
        return it.next();
    }
}

From source file:Main.java

public static boolean isEmptyCollection(Collection<?> cols) {
    if (cols == null || cols.isEmpty()) {
        return true;
    }/*from  w ww  .  java  2 s  .  com*/
    Iterator<?> ite = cols.iterator();
    while (ite.hasNext()) {
        if (ite.next() == null) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static <T, C extends Collection<? super T>> C addTo(final Iterator<T> iter, final C c) {
    while (iter.hasNext())
        c.add(iter.next());//w ww  .j a v a  2s  .  co  m
    return c;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static List<String> keysToList(Map<?, ?> map) {
    List<String> list = new ArrayList<String>();
    Set<?> set = map.entrySet();
    Iterator<?> iter = set.iterator();
    while (iter.hasNext()) {
        Map.Entry m = (Map.Entry) iter.next();
        list.add("" + m.getKey());
    }//from ww w.j a va2 s.  c o  m
    return list;
}

From source file:Main.java

/**
 * Prints each object in the collection with commas between them.
 * Nulls will print as "null".  A null collection will cause NPE.
 *///from  w ww.  j  a v  a  2s.co m
public static String collectionToString(Collection col) {
    StringBuffer outputString = new StringBuffer(100);
    boolean firstItem = true;
    Iterator it = col.iterator();
    while (it.hasNext()) {
        if (!firstItem)
            outputString.append(", ");
        outputString.append(it.next());
        firstItem = false;
    }
    return outputString.toString();
}

From source file:Main.java

public static Object getFirstObject(Collection<?> collection) {
    Iterator<?> iterator = collection.iterator();

    if (iterator.hasNext()) {
        return iterator.next();
    } else {// w  ww  .  j  a  va2  s.c om
        return null;
    }
}

From source file:Main.java

/**
 * Compares two collections returning the number of elements they have in common
 * /*from  ww  w  . ja  va  2  s  .c o m*/
 * @param <T>
 * @param one
 * @param two
 * @return
 */
public static <T> int collectionCompare(Collection<T> one, Collection<T> two) {
    int count = 0;

    Iterator<T> e = one.iterator();
    while (e.hasNext()) {
        if (two.contains(e.next()))
            ++count;
    }

    return count;
}

From source file:Main.java

/**
 * Given an iterable A whose elements are iterables, this method will return the first
 * element of A.//from w  w w  .  j a va 2  s. c o m
 * If no such element exist, an empty iterable is returned rather than null.
 *
 *
 * @param iterable
 * @param <S>
 * @param <T>
 * @return
 */
public static <S, T extends Iterable<S>> Iterable<S> safeGetFirst(Iterable<T> iterable) {
    Iterator<T> it = iterable.iterator();

    return it.hasNext() ? it.next() : Collections.<S>emptySet();
}