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 int cardinality(java.lang.Object obj, Collection coll) {
    int numObject = 0;
    if (coll != null) {
        Iterator iter = coll.iterator();

        while (iter.hasNext()) {
            Object collObj = iter.next();

            if (collObj != null) {
                if (collObj.equals(obj)) {
                    numObject++;// w  w w  . j  a  v a  2  s . co  m
                }
            }

        }
    }
    return numObject;
}

From source file:Main.java

/**
 * From http://stackoverflow.com/questions/3047051/how-to-determine-if-a-list-is-sorted-in-java
 * @param iterable/*w w w.  jav a  2  s .  c  o m*/
 * @param <T>
 * @return
 */
public static <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable) {
    Iterator<T> iter = iterable.iterator();
    if (!iter.hasNext()) {
        return true;
    }
    T t = iter.next();
    while (iter.hasNext()) {
        T t2 = iter.next();
        if (t.compareTo(t2) > 0) {
            return false;
        }
        t = t2;
    }
    return true;
}

From source file:Main.java

/**
 * Joins the {@link String} representation of elements of a given list,
 * dividing with a separator.//from   w  w  w  .ja  v  a2 s . c o  m
 * 
 * @param input
 *            - the {@link List} of elements to join.
 * @param separator
 *            - the separator to place between elements
 * @return - the joined {@link String}
 */
public static <E extends Object> String join(List<E> input, String separator) {
    StringBuilder result = new StringBuilder();
    Iterator<E> it = input.iterator();

    while (it.hasNext()) {
        result.append(it.next());
        if (separator != null && it.hasNext()) {
            result.append(separator);
        }
    }

    return result.toString();
}

From source file:Main.java

/**
 * Append a collection of strings and delimiter.
 *
 * @param c A collection of strings.// w  w  w .java2  s  .  c o  m
 * @param delimiter A delimiter string.
 * @return The new string.
 */
public static String join(Collection<String> c, String delimiter) {
    if (c == null || delimiter == null) {
        throw new IllegalArgumentException("Collections and delimiters given to join cannot be null!");
    }
    StringBuilder builder = new StringBuilder("");
    Iterator<String> iter = c.iterator();
    while (iter.hasNext()) {
        builder.append(iter.next());
        if (iter.hasNext()) {
            builder.append(delimiter);
        }
    }
    return builder.toString();
}

From source file:Main.java

public static <T> T getFirst(Iterable<? extends T> iterable, T def) {
    final Iterator<? extends T> iter = iterable.iterator();
    return iter.hasNext() ? iter.next() : def;
}

From source file:Main.java

/**
 * Returns an arbitrary element from the given collection.
 * //  w ww .  j  ava2 s  . c om
 * @param collection
 *        to retrieve the element from
 * @return an arbitrary element or <tt>null</tt> if the collection is empty
 */
public static <T> T getAny(final Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    }

    return null;
}

From source file:Main.java

public static <E> E getOnlyElement(Iterable<E> iterable) {
    Iterator<E> iterator = iterable.iterator();

    if (!iterator.hasNext()) {
        throw new RuntimeException("Collection is empty");
    }/*w  ww .  java  2  s.  c  o m*/

    E element = iterator.next();

    if (iterator.hasNext()) {
        throw new RuntimeException("Collection contains more than one item");
    }

    return element;
}

From source file:Main.java

public static boolean hasValue(Map<?, ?> map, String key, String val) {
    if (map == null) {
        return false;
    }/*from   ww  w  .  j a  v a2 s  . com*/
    Collection<?> col = (Collection<?>) map.get(key);
    if (col == null) {
        return false;
    }
    Iterator<?> itr = col.iterator();
    while (itr.hasNext()) {
        String str = (String) itr.next();
        if (str.equalsIgnoreCase(val)) {
            return true;
        }
        if (str.toLowerCase().startsWith(val)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static double calcStrSetSimilarity(final Set<String> strASet1, final Set<String> strASet2) {
    int containsCount = 0;
    final Iterator<String> iter = strASet2.iterator();
    while (iter.hasNext()) {
        if (strASet1.contains(iter.next())) {
            containsCount++;//from ww w. j  a va2 s  .c  o m
        }
    }
    return (double) containsCount / (double) strASet1.size();
}

From source file:Main.java

public static <T> List<T> search(final String str, Collection<? extends T> coll, Comparable<? super T> comp) {
    if (comp == null)
        return null;
    List<T> list = new ArrayList<T>();
    Iterator<? extends T> i = coll.iterator();
    while (i.hasNext()) {
        T next = i.next();// w  ww. j av a 2 s  .  c om
        if (comp.compareTo(next) > 0) {
            list.add(next);
        }
    }
    return list;
}