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> 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());/*from w w  w . j  av  a2s  .  c  o m*/
        k--;
    }
    return tmp;
}

From source file:Main.java

/**
 * HashMap Operations/*from  ww w.j a v a2  s.c  o m*/
 * */
public static void printHash(HashMap<String, Integer> hashMap) {
    System.out.println("Print HashMap");
    Set<Map.Entry<String, Integer>> s = hashMap.entrySet();
    Iterator<Map.Entry<String, Integer>> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry<String, Integer> m = (Map.Entry<String, Integer>) it.next();
        System.out.println(m.getKey() + "\t" + m.getValue());
    }
}

From source file:Main.java

/**
 * Returns a String representation of the elements delimited with the specified delimiter 
 * //from   ww w . j  av a2 s.  com
 * @param elements the elements to build the String out of
 * @param delimiter the specified delimiter
 * @return the delimited String
 */
public static <T> String asDelimitedString(Collection<T> elements, String delimiter) {
    StringBuilder sb = new StringBuilder();
    Iterator<T> iterator = elements.iterator();
    while (iterator.hasNext()) {
        sb.append(iterator.next().toString());
        sb.append(iterator.hasNext() ? delimiter : EMPTY_STRING);
    }
    return sb.toString();
}

From source file:Main.java

public static <E> E first(Collection<E> collection) {

    if (isEmpty(collection)) {
        return null;
    }//  ww w  .ja  v a  2s. c  om

    Iterator<E> iterator = collection.iterator();

    if (iterator.hasNext()) {
        return iterator.next();
    }

    throw new IllegalStateException("Should not happen");
}

From source file:Main.java

public static <T> Collection<T> reverse(final Collection<T> collection) {
    final LinkedList<T> newCollection = new LinkedList<>();
    final Iterator<T> i = collection.iterator();
    while (i.hasNext()) {
        newCollection.addFirst(i.next());
    }/* ww  w  . j  a  v  a 2  s. co  m*/
    return newCollection;
}

From source file:Main.java

private static <T extends Comparable<T>> T getExtremum(Collection<T> vals, int comparator) {
    if (vals.size() == 0)
        return null;
    Iterator<T> it = vals.iterator();
    T ret = it.next();//from  ww  w  .ja va  2s  .  c  o  m
    while (it.hasNext()) {
        T v = it.next();
        if (v.compareTo(ret) * comparator > 0)
            ret = v;
    }
    return ret;
}

From source file:Main.java

/**
 * Returns the first object of given type or its subtypes
 * @param clazz The class you want to return the first instance found of.
 * @param objects The collection you'd like to search.
 * @return The first instance of a specific Class found in a Collection.  Returns null if no instance is found.
 *///from   ww  w . j av  a  2s. c om
public static Object getFirstInstance(Class clazz, Collection objects) {
    if (objects != null && clazz != null) {
        Iterator objectsIterator = objects.iterator();
        while (objectsIterator.hasNext()) {
            Object instance = objectsIterator.next();
            if (clazz.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * patterns: ["/util//note"]//from w  w  w  .j  a  va  2s  .  c o m
 * @param Set<String> patterns
 * @return /util/note
 */
public static String first(Set<String> patterns) {
    String str = "";
    if (patterns != null && patterns.size() > 0) {
        Iterator<String> iterator = patterns.iterator();
        while (iterator.hasNext()) {
            str = (String) iterator.next();
            str = str.replaceAll("//", "/");
            break;
        }
    }
    return str;
}

From source file:Main.java

public static String buildValuesString(List<Object> values) {
    StringBuilder sb = new StringBuilder();
    Iterator<Object> valueIt = values.iterator();
    while (valueIt.hasNext()) {
        sb.append(valueIt.next());/*  w w w . j  a  v  a 2s. c o m*/
        if (valueIt.hasNext()) {
            sb.append(";");
        }
    }
    return sb.toString();
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
static HashMap<String, Object> toMap(JSONObject object) throws JSONException {
    if (object == null) {
        return null;
    }//from  w ww  .  j  a  va2s.co  m
    HashMap<String, Object> map = new HashMap();
    Iterator keys = object.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        map.put(key, fromJson(object.get(key)));
    }
    return map;
}