Example usage for java.util Collection size

List of usage examples for java.util Collection size

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:Main.java

public static boolean isEmpty(Collection<?> collection) {
    return (collection == null) || (collection.size() == 0);
}

From source file:Main.java

/**
 * Splits a collection in <tt>n</tt> new collections. The last collection may contain more elements than the others
 * if {@code set.size() % n != 0}.// w ww  .j ava  2  s .  c o m
 *
 * @param set the input collection
 * @param n   the number of new collections
 * @param <T> the element type
 * @return a list containing the new collections
 */
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());
            }
            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:Main.java

public static int getSize(final Collection<?> collection) {
    if (collection == null) {
        return 0;
    }//from   w ww  .ja v  a2  s . c  o m
    return collection.size();
}

From source file:edu.northwestern.bioinformatics.studycalendar.web.taglibs.Functions.java

public static Collection pluck(Collection collection, String property) {
    Collection result = new ArrayList(collection.size());
    for (Object o : collection) {
        try {//from w ww .j  a  v a 2  s . c  om
            Object plucked = new Expression(o, "get" + capitalize(property), null).getValue();
            result.add(plucked);
        } catch (Exception e) {
            throw new StudyCalendarError("Unable to get the '" + property + "' property", e);
        }

    }
    return result;
}

From source file:Main.java

/**
 * clone all elements/*from   w  ww  .ja va 2s . c o m*/
 * 
 * @param inputCollection
 *            the collection to get the input from, may not be null
 * @return all elements (new list)
 * @throws NullPointerException
 *             if the input collection is null
 */
public static <E> Collection<E> cloneEx(Collection<E> inputCollection) {
    final ArrayList<E> outputCollection = new ArrayList<E>(inputCollection.size());
    for (Iterator<E> iter = inputCollection.iterator(); iter.hasNext();) {
        outputCollection.add(iter.next());
    }
    return outputCollection;
}

From source file:Main.java

/**
 * Returns the number of items in the given collection.
 *
 * @param collection    The collection to return the length of.
 * @param <E>           The component type of the items stored in the collection.
 * @return              The number of items in the given collection.
 */// ww  w  . ja  va2 s . c  om
public static <E> int length(Collection<E> collection) {
    if (collection == null)
        return 0;
    return collection.size();
}

From source file:Main.java

public static Collection<String> convertLongsToStrings(Collection<Long> longs) {
    if (longs != null) {
        Collection<String> strings = new ArrayList<String>(longs.size());
        for (long longNumber : longs) {
            strings.add(String.valueOf(longNumber));
        }/*  w w w.j  a va  2 s . co m*/
        return strings;
    }
    return null;
}

From source file:com.nec.strudel.bench.test.EntityAssert.java

public static <E> void assertSameEntitySets(Collection<E> expected, Collection<E> actual) {
    assertEquals(expected.size(), actual.size());
    for (E e : actual) {
        assertContains(e, expected);/*from ww w.j  av  a 2s  .c  om*/
    }
}

From source file:Main.java

/**
 * Returns an array containing all of the elements in the given collection. This is a
 * compile-time type-safe alternative to {@link Collection#toArray(Object[])}.
 * /*w w  w .j  av  a2s .  c  o m*/
 * @param collection the source collection
 * @param clazz the type of the array elements
 * @param <A> the type of the array elements
 * @return an array of type <code>A</code> containing all of the elements in the given
 *         collection
 * 
 * @throws NullPointerException if the specified collection or class is null
 * 
 * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7023484">Sun bug 7023484</a>
 */
public static <A> A[] toArray(Collection<? extends A> collection, Class<A> clazz) {
    Object array = Array.newInstance(clazz, collection.size());
    @SuppressWarnings("unchecked")
    A[] typedArray = collection.toArray((A[]) array);
    return typedArray;
}

From source file:Main.java

public static int[] toArray(Collection<Integer> col) {
    if (col == null)
        return null;
    int[] arr = new int[col.size()];
    int i = 0;//from  w  w w.  j av a2  s  . c  o m
    for (Integer v : col)
        arr[i++] = v.intValue();
    return arr;
}