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

/**
 * Creates new collection that contains only element whose string
 * representation starts with prefix It uses raw collection - not
 * parameterized. There is no {@link SuppressWarnings} annotation so IDE
 * will probably report rawtype and/or unchecked warnings.
 * //from   w w w. j a v a2  s  .co m
 * @param c
 *            raw collection to create prefixed collection from
 * @param prefix
 *            to use as filter
 * @return collection without nulls
 */
public static Collection withPrefixRaw(Collection c, String prefix) {
    Collection prefixed = new ArrayList();
    Iterator iterator = c.iterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        if (o != null && o.toString().startsWith(prefix)) {
            prefixed.add(o);
        }
    }
    return prefixed;
}

From source file:Main.java

public static JSONObject shallowDiffJSONObjects(JSONObject jo1, JSONObject jo2) throws JSONException {
    JSONObject result = new JSONObject();
    Iterator<String> it = jo2.keys();
    while (it.hasNext()) {
        String key = it.next();//from  ww w  .  j  a v a2s . c  om

        // See if the key is missing or the value is different in the other object
        Object val1 = jo1.opt(key);
        Object val2 = jo2.get(key);

        if (val1 == null || !val2.equals(val1))
            result.put(key, val2);
    }

    return result;
}

From source file:Main.java

/**
 * Creates new collection that contains only element whose string
 * representation starts with prefix It is parameterized.
 * //from  www .j  a v a2  s  .com
 * @param c
 *            raw collection to create prefixed collection from
 * @param prefix
 *            to use as filter
 * @return collection without nulls
 */
public static <T> Collection<T> withPrefix(Collection<T> c, String prefix) {
    Collection<T> prefixed = new ArrayList<T>();
    Iterator<T> iterator = c.iterator();
    while (iterator.hasNext()) {
        T o = iterator.next();
        if (o != null && o.toString().startsWith(prefix)) {
            prefixed.add(o);
        }
    }
    return prefixed;
}

From source file:Main.java

public static boolean containsInstance(Collection collection, Object element) {
    if (collection != null) {
        Iterator it = collection.iterator();

        while (it.hasNext()) {
            Object candidate = it.next();
            if (candidate == element) {
                return true;
            }//  ww  w  .j  a va2  s  .c o  m
        }
    }

    return false;
}

From source file:Main.java

/**
 * Returns a {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>. The
 * cardinality of each element <i>e</i> in the returned {@link Collection}
 * will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality of
 * <i>e</i> in <i>b</i>, or zero, whichever is greater.
 * //ww w  .  j  a  v a  2 s .  c o  m
 * @see Collection#removeAll
 */
public static Collection subtract(final Collection a, final Collection b) {
    ArrayList list = new ArrayList(a);
    Iterator it = b.iterator();
    while (it.hasNext()) {
        list.remove(it.next());
    }
    return list;
}

From source file:Main.java

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

From source file:Main.java

public static <T> T removeFrom(Iterable<T> collection, Predicate<T> predicate) {
    Iterator<T> it = collection.iterator();

    while (it.hasNext()) {
        T o = it.next();/*  w w w . j a va 2 s  .  c o  m*/

        if (predicate.test(o)) {
            it.remove();
            return o;
        }
    }

    return null;
}

From source file:Main.java

public static void removeItemFromCollectionByUsingIteratorRemoveMethod(Collection<String> collection,
        String elementToBeRemoved) {

    Iterator iterator = collection.iterator();

    while (iterator.hasNext()) {
        String element = (String) iterator.next();

        if (element.equals(elementToBeRemoved)) {
            iterator.remove();/*from ww w. ja  v a  2s .  co  m*/
        }
    }
}

From source file:Main.java

public static boolean containsInstance(Collection collection, Object element) {
    if (collection != null) {
        Iterator i$ = collection.iterator();

        while (i$.hasNext()) {
            Object candidate = i$.next();
            if (candidate == element) {
                return true;
            }/*from   w w w . j  a  v a2  s.  com*/
        }
    }

    return false;
}

From source file:Main.java

public static boolean isServiceRunning(Context ctx, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List servicesList = activityManager.getRunningServices(2147483647);
    Iterator l = servicesList.iterator();
    while (l.hasNext()) {
        ActivityManager.RunningServiceInfo si = (ActivityManager.RunningServiceInfo) l.next();
        if (className.equals(si.service.getClassName())) {
            isRunning = true;/*  w w w.j  a v  a 2  s  .  c  om*/
        }
    }
    return isRunning;
}