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:com.kelveden.rastajax.cli.Runner.java

private static File findWar() {
    final Collection<File> wars = FileUtils.listFiles(new File(System.getProperty("user.dir")),
            new String[] { "war" }, true);

    LOGGER.info("Found the following wars: " + wars.toString());

    if (wars.size() > 0) {
        return wars.iterator().next();
    } else {// w  ww. j  a va2  s  . com
        return null;
    }
}

From source file:Main.java

public static <T> List<T>[] split(Collection<T> set, int n) {
    if (set.size() >= n) {
        @SuppressWarnings("unchecked")
        List<T>[] arrays = new List[n];
        int minSegmentSize = (int) Math.floor(set.size() / (double) n);

        int start = 0;
        int stop = minSegmentSize;

        Iterator<T> it = set.iterator();

        for (int i = 0; i < n - 1; i++) {
            int segmentSize = stop - start;
            List<T> segment = new ArrayList<T>(segmentSize);
            for (int k = 0; k < segmentSize; k++) {
                segment.add(it.next());//w w w.  j  a v  a2  s . c  o  m
            }
            arrays[i] = segment;
            start = stop;
            stop += segmentSize;
        }

        int segmentSize = set.size() - start;
        List<T> segment = new ArrayList<T>(segmentSize);
        for (int k = 0; k < segmentSize; k++) {
            segment.add(it.next());
        }
        arrays[n - 1] = segment;

        return arrays;
    } else {
        throw new IllegalArgumentException("n must not be smaller set size!");
    }
}

From source file:com.neusoft.mid.clwapi.tools.JacksonUtils.java

/**
 * String?/*from   w  ww .  j  a  v a2 s  .c o m*/
 * 
 * @param obj
 *            ?(?JavaBean,String,Collection,Map)
 * @return ??
 * @throws JacksonEncoderException
 *             jackson?
 */
public static Object jsonEncoder(Object obj) throws JacksonEncoderException {
    logger.debug("obj class is:" + obj.getClass());
    if (obj instanceof String) {
        return jsonCoder((String) obj, true);
    } else if (obj instanceof Map<?, ?>) {
        logger.debug("Object is " + obj.getClass());
        Set<?> entries = ((Map<?, ?>) obj).entrySet();
        Iterator<?> it = entries.iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
            logger.debug("Key is [" + entry.getKey() + "]");
            entry.setValue(jsonEncoder(entry.getValue()));
        }
    } else if (obj instanceof Collection<?>) {
        logger.debug("Object is " + obj.getClass());
        Collection<?> c = (Collection<?>) obj;
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            jsonEncoder(it.next());
        }
    } else {
        try {
            Field[] fields = obj.getClass().getFields();
            for (Field f : fields) {
                jsonCoder(obj, f);
            }
        } catch (IllegalAccessException e) {
            logger.error("json??" + e.getMessage());
            throw new JacksonEncoderException(e);
        }
    }
    return obj;
}

From source file:kina.utils.UtilMongoDB.java

/**
 * converts from an entity class with kina's anotations to BsonObject.
 *
 * @param t   an instance of an object of type T to convert to BSONObject.
 * @param <T> the type of the object to convert.
 * @return the provided object converted to BSONObject.
 * @throws IllegalAccessException//  w ww.  ja  v  a 2s  .  c o  m
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static <T extends KinaType> BSONObject getBsonFromObject(T t)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Field[] fields = filterKinaFields(t.getClass());

    BSONObject bson = new BasicBSONObject();

    for (Field field : fields) {
        Method method = Utils.findGetter(field.getName(), t.getClass());
        Object object = method.invoke(t);
        if (object != null) {
            if (Collection.class.isAssignableFrom(field.getType())) {
                Collection c = (Collection) object;
                Iterator iterator = c.iterator();
                List<BSONObject> innerBsonList = new ArrayList<>();

                while (iterator.hasNext()) {
                    innerBsonList.add(getBsonFromObject((KinaType) iterator.next()));
                }
                bson.put(kinaFieldName(field), innerBsonList);
            } else if (KinaType.class.isAssignableFrom(field.getType())) {
                bson.put(kinaFieldName(field), getBsonFromObject((KinaType) object));
            } else {
                bson.put(kinaFieldName(field), object);
            }
        }
    }

    return bson;
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.publico.ReadStudentsAndGroupsByShiftID.java

@Atomic
public static InfoSiteStudentsAndGroups run(String groupPropertiesId, String shiftId)
        throws FenixServiceException {
    InfoSiteStudentsAndGroups infoSiteStudentsAndGroups = new InfoSiteStudentsAndGroups();

    Grouping groupProperties = FenixFramework.getDomainObject(groupPropertiesId);
    infoSiteStudentsAndGroups.setInfoGrouping(InfoGrouping.newInfoFromDomain(groupProperties));
    Shift shift = FenixFramework.getDomainObject(shiftId);

    if (groupProperties == null) {
        throw new ExistingServiceException();
    }//from w ww .j  ava  2s. com

    infoSiteStudentsAndGroups.setInfoShift(InfoShift.newInfoFromDomain(shift));
    List infoSiteStudentsAndGroupsList = new ArrayList();
    List studentGroups = getStudentGroupsByShiftAndGroupProperties(groupProperties, shift);
    Iterator iterStudentGroups = studentGroups.iterator();
    while (iterStudentGroups.hasNext()) {

        Collection studentGroupAttendList;
        StudentGroup studentGroup = (StudentGroup) iterStudentGroups.next();

        studentGroupAttendList = studentGroup.getAttendsSet();

        Iterator iterStudentGroupAttendList = studentGroupAttendList.iterator();
        InfoSiteStudentInformation infoSiteStudentInformation = null;
        InfoSiteStudentAndGroup infoSiteStudentAndGroup = null;
        Attends attend = null;

        while (iterStudentGroupAttendList.hasNext()) {
            infoSiteStudentInformation = new InfoSiteStudentInformation();
            infoSiteStudentAndGroup = new InfoSiteStudentAndGroup();

            attend = (Attends) iterStudentGroupAttendList.next();

            infoSiteStudentAndGroup.setInfoStudentGroup(InfoStudentGroup.newInfoFromDomain(studentGroup));

            infoSiteStudentInformation.setNumber(attend.getRegistration().getNumber());

            infoSiteStudentInformation.setName(attend.getRegistration().getPerson().getName());

            infoSiteStudentInformation.setUsername(attend.getRegistration().getPerson().getUsername());

            infoSiteStudentInformation.setEmail(attend.getRegistration().getPerson().getEmail());

            infoSiteStudentInformation.setPersonID(attend.getRegistration().getPerson().getExternalId());

            infoSiteStudentAndGroup.setInfoSiteStudentInformation(infoSiteStudentInformation);

            infoSiteStudentsAndGroupsList.add(infoSiteStudentAndGroup);
        }
    }

    Collections.sort(infoSiteStudentsAndGroupsList, new BeanComparator("infoSiteStudentInformation.number"));

    infoSiteStudentsAndGroups.setInfoSiteStudentsAndGroupsList(infoSiteStudentsAndGroupsList);
    return infoSiteStudentsAndGroups;
}

From source file:Main.java

public static <T extends Comparable<T>> int compareAsSet(Collection<? extends T> set1,
        Collection<? extends T> set2) {
    int res = Ints.compare(set1.size(), set2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> sortedSet1 = toNaturalSortedSet(set1);
    SortedSet<? extends T> sortedSet2 = toNaturalSortedSet(set2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }/*from ww w  .j a v a  2  s.  com*/
    return 0;
}

From source file:ArrayHelper.java

public static int[] toIntArray(Collection coll) {
    Iterator iter = coll.iterator();
    int[] arr = new int[coll.size()];
    int i = 0;/* w ww . ja  v  a 2 s  .c  o  m*/
    while (iter.hasNext()) {
        arr[i++] = ((Integer) iter.next()).intValue();
    }
    return arr;
}

From source file:ArrayHelper.java

public static boolean[] toBooleanArray(Collection coll) {
    Iterator iter = coll.iterator();
    boolean[] arr = new boolean[coll.size()];
    int i = 0;//  w w w . j  a va  2s .  com
    while (iter.hasNext()) {
        arr[i++] = ((Boolean) iter.next()).booleanValue();
    }
    return arr;
}

From source file:Main.java

public static <T extends Comparable<T>> int compareAsSet(@Nonnull Collection<? extends T> set1,
        @Nonnull Collection<? extends T> set2) {
    int res = Ints.compare(set1.size(), set2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> sortedSet1 = toNaturalSortedSet(set1);
    SortedSet<? extends T> sortedSet2 = toNaturalSortedSet(set2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }/* w  ww.jav a2s.co  m*/
    return 0;
}

From source file:kina.utils.UtilMongoDB.java

/**
 * converts from and entity class with kina's anotations to BsonObject
 *
 * @return/*from  w w w  .j  a v a  2  s.  c  o m*/
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static BSONObject getBsonFromCell(Cells cells)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {

    BSONObject bson = new BasicBSONObject();
    for (Cell cell : cells) {
        if (List.class.isAssignableFrom(cell.getCellValue().getClass())) {
            Collection c = (Collection) cell.getCellValue();
            Iterator iterator = c.iterator();
            List<BSONObject> innerBsonList = new ArrayList<>();

            while (iterator.hasNext()) {
                innerBsonList.add(getBsonFromCell((Cells) iterator.next()));
            }
            bson.put(cell.getCellName(), innerBsonList);
        } else if (Cells.class.isAssignableFrom(cell.getCellValue().getClass())) {
            bson.put(cell.getCellName(), getBsonFromCell((Cells) cell.getCellValue()));
        } else {
            bson.put(cell.getCellName(), cell.getCellValue());
        }

    }

    return bson;
}