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 byte[] toByteArray(Collection<? extends Number> c) {
    byte arr[] = new byte[c.size()];
    int i = 0;/*from w ww .j a v a 2 s  .c  o m*/
    for (Number item : c)
        arr[i++] = item.byteValue();
    return arr;
}

From source file:Main.java

/**
 * @param coll//from   w  ww . jav  a  2  s  .  c o m
 * @postcondition (coll == null) --> (result == 0)
 * @postcondition (coll != null) --> (result == coll.size())
 */
public static int size(Collection<?> coll) {
    return (coll == null) ? 0 : coll.size();
}

From source file:Main.java

public static <T> List<T> shallowCopyToList(Collection<T> collection) {
    ArrayList<T> copied = new ArrayList<T>(collection.size());
    for (T element : collection) {
        copied.add(element);//from   ww w.j a v a 2  s  . c  o m
    }
    return copied;
}

From source file:Main.java

public static boolean isNotEmpty(Collection<?> coll) {
    return coll != null && coll.size() > 0;
}

From source file:Main.java

public static void trimCollection(Collection collection, int numberOfElements) {
    if (collection.size() < numberOfElements) {
        return;/*from  w  w w  .  j a  v  a  2 s.  c  o m*/
    }

    numberOfElements = collection.size() - numberOfElements;
    Iterator it = collection.iterator();
    int counter = 0;

    while (it.hasNext()) {
        if (counter <= numberOfElements) {
            it.next();
            counter++;
        }
        it.remove();
    }
}

From source file:Main.java

public static Integer sum(Collection<Integer> collection) {
    if (collection == null || collection.size() == 0) {
        return 0;
    }// w w w  . j  av a  2 s  .  com

    Integer answer = 0;

    for (Integer value : collection) {
        answer += value;
    }

    return answer;
}

From source file:Main.java

public static short[] toShortArray(Collection<? extends Number> c) {
    short arr[] = new short[c.size()];
    int i = 0;// www .  j  a va 2s.  c om
    for (Number item : c)
        arr[i++] = item.shortValue();
    return arr;
}

From source file:Main.java

public static float[] toFloatArray(Collection<? extends Number> c) {
    float arr[] = new float[c.size()];
    int i = 0;//w ww. j  a va2s  .c om
    for (Number item : c)
        arr[i++] = item.floatValue();
    return arr;
}

From source file:Main.java

/**
 * Cast a collection to an array/*  w ww.  j  a  v a  2 s .c o m*/
 *
 * @param collection the collection to cast
 * @param <T>        the type of the collection contents
 * @return an array of type T
 */
public static <T> T[] toArray(Collection<T> collection) {
    return (T[]) collection.toArray(new Object[collection.size()]);
}

From source file:Main.java

/**
 * Checks if collection is null or empty
 *
 * @param source collection to check/*from   w w w  .j a va2s.c  om*/
 * @return true if collection is null or empty
 */
public static boolean isEmpty(Collection<?> source) {
    return source == null || source.size() == 0;
}