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 boolean hasUniqueObject(Collection collection) {
    if (isEmpty(collection)) {
        return false;
    } else {/*from w ww. j a v a  2s  .c  o m*/
        boolean hasCandidate = false;
        Object candidate = null;
        Iterator i$ = collection.iterator();

        while (i$.hasNext()) {
            Object elem = i$.next();
            if (!hasCandidate) {
                hasCandidate = true;
                candidate = elem;
            } else if (candidate != elem) {
                return false;
            }
        }

        return true;
    }
}

From source file:com.projity.util.DataUtils.java

public static void extractObjectsOfClassFromNodeList(Collection result, Collection nodeList,
        Class objectClass) {//from  w w w .j av a2s . c  o m
    result.clear();
    Iterator i = nodeList.iterator();
    Object nodeObject;

    while (i.hasNext()) {
        nodeObject = ((Node) i.next()).getImpl();
        nodeObject = DataUtils.extractObjectOfClass(nodeObject, objectClass);
        if (nodeObject != null) {
            if (!result.contains(nodeObject)) // only add if not already in there
                result.add(nodeObject);
        }

    }
}

From source file:Main.java

public static boolean hasUniqueObject(Collection<?> collection) {
    if (isEmpty(collection)) {
        return false;
    }//from www.  ja  v  a  2  s.c  om
    boolean hasCandidate = false;
    Object candidate = null;
    for (Iterator<?> it = collection.iterator(); it.hasNext();) {
        Object elem = it.next();
        if (!hasCandidate) {
            hasCandidate = true;
            candidate = elem;
        } else if (candidate != elem) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static Object containsClassAttributeInCollection(Collection in, String fieldName, Object value)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (value instanceof Boolean || value == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$

    while (it.hasNext()) {
        Object obj = it.next();/*from  w  w w . ja v a  2  s.  c o m*/
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        if (value != null && value2 != null && value.equals(value2))
            return obj;
    }
    return null;
}

From source file:Main.java

public static <T> T getSoleElement(final Collection<T> items) {
    if (items.size() != 1)
        throw new IllegalArgumentException(
                String.format("Expected a single element in %s, but found %s.", items, items.size()));
    return items.iterator().next();
}

From source file:es.molabs.io.utils.FileHelper.java

public static URL[] getFiles(URL path, boolean recursive, String... extensions) throws IOException {
    URL[] urls = null;//www  . j  a v  a  2s .c  o m

    try {
        Collection<File> fileCollection = FileUtils.listFiles(new File(path.toURI()), extensions, recursive);
        urls = new URL[fileCollection.size()];

        Iterator<File> iterator = fileCollection.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            File file = iterator.next();

            urls[index++] = file.toURI().toURL();
        }
    } catch (URISyntaxException USe) {
        throw new IOException(USe);
    }

    return urls;
}

From source file:net.sf.zekr.common.util.CollectionUtils.java

public static String toString(Collection<?> collection, String delim) {
    StringBuffer buf = new StringBuffer();
    Iterator<?> i = collection.iterator();
    if (i.hasNext())
        buf.append(String.valueOf(i.next()));
    while (i.hasNext()) {
        buf.append(delim);/*from  w w  w .ja  v a  2 s  . co  m*/
        buf.append(String.valueOf(i.next()));
    }
    return buf.toString();
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.utils.RequestUtils.java

public static Collection buildExecutionDegreeLabelValueBean(Collection executionDegrees) {
    final Map duplicateDegreesMap = new HashMap();
    for (Iterator iterator = executionDegrees.iterator(); iterator.hasNext();) {
        InfoExecutionDegree infoExecutionDegree = (InfoExecutionDegree) iterator.next();
        InfoDegree infoDegree = infoExecutionDegree.getInfoDegreeCurricularPlan().getInfoDegree();
        String degreeName = infoDegree.getNome();

        if (duplicateDegreesMap.get(degreeName) == null) {
            duplicateDegreesMap.put(degreeName, new Boolean(false));
        } else {//from   ww w.jav a  2 s  .c  om
            duplicateDegreesMap.put(degreeName, new Boolean(true));
        }
    }

    Collection lableValueList = CollectionUtils.collect(executionDegrees, new Transformer() {

        @Override
        public Object transform(Object arg0) {
            InfoExecutionDegree infoExecutionDegree = (InfoExecutionDegree) arg0;

            String label = infoExecutionDegree.getInfoDegreeCurricularPlan().getDegreeCurricularPlan()
                    .getPresentationName(infoExecutionDegree.getInfoExecutionYear().getExecutionYear());

            String value = infoExecutionDegree.getExternalId().toString();

            return new LabelValueBean(label, value);
        }

    });

    Comparator comparator = new BeanComparator("label", Collator.getInstance());
    Collections.sort((List) lableValueList, comparator);

    return lableValueList;
}

From source file:Main.java

public static <T> T getObjInList(Class<T> clazz, Collection<T> all, String valueStr, String propName)
        throws Exception {
    if (all.isEmpty() || valueStr == null) {
        return null;
    }//from w  w  w  .  j  a  va2  s  .  c om
    T obj = null;
    Iterator<T> iter = all.iterator();
    for (; iter.hasNext();) {
        T temp = iter.next();
        Object match = null;
        if (propName == null) {
            match = temp.toString();
        } else {
            Field field = clazz.getField(propName);
            field.setAccessible(true);
            match = field.get(temp);
        }
        if (valueStr.equals(match)) {
            obj = temp;
            break;
        }
    }
    return obj;
}

From source file:dev.meng.wikipedia.profiler.util.StringUtils.java

public static String collectionToString(Collection collection, String delimeter) {
    String result = "";
    if (!collection.isEmpty()) {
        Iterator iterator = collection.iterator();
        result = result + iterator.next();
        while (iterator.hasNext()) {
            result = result + delimeter + iterator.next();
        }//  w ww.  ja  va2s  .  co  m
    }
    return result;
}