Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

In this page you can find the example usage for java.util Collection iterator.

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:Main.java

public static String deepToString(final Collection<?> collection, final String left, final String right,
        final String sep) {
    final StringBuilder sb = new StringBuilder(3 * collection.size());
    sb.append(left);//from   w  ww . j  a  v a  2s. co m
    final Iterator<?> iter = collection.iterator();
    if (iter.hasNext())
        sb.append(iter.next());
    while (iter.hasNext()) {
        sb.append(sep);
        sb.append(iter.next());
    }
    sb.append(right);
    return sb.toString();
}

From source file:edu.ksu.cis.indus.tools.slicer.SlicerToolHelper.java

/**
 * Optimizes the slice calculated by the given tool for space. This method should be called after residualization.
 * // ww w .  j  a  v a  2s  . c o  m
 * @param tool in which the slice should be optimized.
 * @param classesToRetain is the collection of FQN of classes that need to be retained in the slice.
 * @return the unspecified classes that were retained.
 * @pre tool != null and classesToRetain != null
 * @post result != null
 */
public static Collection<SootClass> optimizeForSpaceAfterResidualization(final SlicerTool<?> tool,
        final Collection<String> classesToRetain) {
    final Collection<SootClass> _classesToErase = new HashSet<SootClass>();
    final Collection<SootClass> _classes = tool.getSystem().getClasses();
    final Iterator<SootClass> _i = _classes.iterator();
    final int _iEnd = _classes.size();

    for (int _iIndex = 0; _iIndex < _iEnd; _iIndex++) {
        final SootClass _sc = _i.next();

        if (_sc.getMethods().size() == 0 && _sc.getFields().size() == 0
                && !classesToRetain.contains(_sc.getName())) {
            _classesToErase.add(_sc);
        }
    }

    final Collection<SootClass> _c = Util.eraseClassesFrom(_classesToErase, tool.getSystem());
    _classesToErase.removeAll(_c);
    return _c;
}

From source file:com.googlecode.jtiger.modules.ecside.table.calc.CalcUtils.java

public static void eachRowCalcValue(CalcHandler handler, Collection rows, String property) {
    if (rows == null) {
        return;//w  w w.  j a  va2s .c o m
    }

    for (Iterator listIter = rows.iterator(); listIter.hasNext();) {
        Object row = listIter.next();
        Object value = null;

        if (ExtremeUtils.isBeanPropertyReadable(row, property)) {
            try {
                value = PropertyUtils.getProperty(row, property);

                if (value instanceof Number) {
                    handler.processCalcValue((Number) value);
                } else {
                    handler.processCalcValue(getValue(property, value));
                }
            } catch (Exception e) {
                String errorMessage = "Problem parsing numeric value for property [" + property + "].";
                logger.warn("CalcUtils.eachCalc() - " + errorMessage);
            }
        }
    }
}

From source file:Main.java

/**
 * Enforce that a Collection contains no more than one element and return that element
 * if it exists./*from w w w .j a  va2s  .  c om*/
 * 
 * @param collection the Collection to examine
 * @return null if the Collection is null or empty, the Collection's single object if
 *       its size is one, or throw if its size is greater than one.
 * @throws IllegalArgumentException if the Collection contains more than one element
 */
public static <T> T singleObject(Collection<T> collection) {
    if (collection == null || collection.isEmpty())
        return null;
    if (collection.size() > 1)
        throw new IllegalArgumentException("more than one element in collection");
    return collection.iterator().next();
}

From source file:Main.java

/**
 * Counts how often the given Object occurs in the given
 * collection using equals() for comparison.
 *
 * @param c collection in which to search
 * @param o object to search/* w  w w  .j av a 2  s  .c o m*/
 * @return frequency
 * @since Ant 1.8.0
 */
public static int frequency(Collection<?> c, Object o) {
    // same as Collections.frequency introduced with JDK 1.5
    int freq = 0;
    if (c != null) {
        for (Iterator<?> i = c.iterator(); i.hasNext();) {
            Object test = i.next();
            if (o == null ? test == null : o.equals(test)) {
                freq++;
            }
        }
    }
    return freq;
}

From source file:Main.java

/**
 * To get a string representation of a collection.
 *
 * @param list//from   www  .  j  a v a2  s.co  m
 *          input list
 * @param delimiter
 *          the separator used between elements
 * @param quoteStrings
 *          <code>true</code> to get quoted representations of all not numbers.
 * @return a string representation of the given collection.
 */
public static <T> String format(Collection<T> list, String delimiter, boolean quoteStrings) {
    if (isEmpty(list)) {
        return "";
    }
    StringBuilder result = new StringBuilder();
    Iterator<T> it = list.iterator();
    // first
    result.append(objectToString(it.next(), quoteStrings));
    // rest
    while (it.hasNext()) {
        result.append(delimiter);
        result.append(objectToString(it.next(), quoteStrings));
    }
    return result.toString();
}

From source file:Main.java

/**
 * Modify the specified Collection so that only the first <code>limit</code> elements
 * remain, as determined by iteration order. If the Collection is smaller than limit,
 * it is unmodified.//from ww w .jav a 2  s. c  om
 */
public static void limit(Collection<?> col, int limit) {
    int size = col.size();
    if (size > limit) {
        if (col instanceof List<?>) {
            ((List<?>) col).subList(limit, size).clear();

        } else {
            Iterator<?> itr = col.iterator();
            for (int ii = 0; ii < limit; ii++) {
                itr.next();
            }
            while (itr.hasNext()) {
                itr.next();
                itr.remove();
            }
        }
    }
}

From source file:CollectionUtils.java

/**
 * Check whether the given Collection contains the given element instance.
 * <p>Enforces the given instance to be present, rather than returning
 * <code>true</code> for an equal element as well.
 * @param collection the Collection to check
 * @param element the element to look for
 * @return <code>true</code> if found, <code>false</code> else
 *//*from   www  . j  a  v  a2  s  . c  om*/
public static boolean containsInstance(Collection collection, Object element) {
    if (collection != null) {
        for (Iterator it = collection.iterator(); it.hasNext();) {
            Object candidate = it.next();
            if (candidate == element) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static Object findOne(Collection coll) {
    if (coll.isEmpty()) {
        return null;
    } else if (coll.size() > 1) {
        throw new RuntimeException("Expected only one member in collection, found many: " + coll.toString());
    } else {/*from   ww  w  .  j a va2s. c o m*/
        return coll.iterator().next();
    }
}

From source file:com.verigreen.common.utils.CollectionUtils.java

public static String toString(Collection<?> data, String collectonName) {
    StringBuilder builder = new StringBuilder();
    builder.append(collectonName).append(" = ").append("{");
    if (data != null) {
        Iterator<?> iter = data.iterator();
        while (iter.hasNext()) {
            builder.append(iter.next());
            if (iter.hasNext()) {
                builder.append(", ");
            }//from  www .j  av a 2 s. com
        }
    }
    builder.append("}");
    return builder.toString();
}