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

/**
 * /*  ww w.  ja  v  a 2  s .c o m*/
 * @param collection
 * @return float[]
 */
public static float[] toFloatArray(final Collection<Float> collection) {
    final float[] array = new float[collection == null ? 0 : collection.size()];

    if (collection != null) {
        int i = 0;

        for (Iterator<Float> iterator = collection.iterator(); iterator.hasNext();) {
            array[i++] = iterator.next().floatValue();
        }
    }

    return array;
}

From source file:Main.java

public static String[] toStringArray(Collection<String> collection) {
    if (collection == null) {
        return null;
    }//from w  w  w  .j ava2s  . c  o m
    return collection.toArray(new String[collection.size()]);
}

From source file:Main.java

public static <T> T[] toArray(Collection<T> list, IntFunction<T[]> action) {
    return list.toArray(action.apply(list.size()));
}

From source file:Main.java

public static <O> O getRandom(final Collection<O> pObjects) {
    if (pObjects != null && !pObjects.isEmpty()) {
        int r = Math.abs(random.nextInt() % pObjects.size());
        List<O> objects = !(pObjects instanceof List) ? new ArrayList<O>(pObjects) : (List<O>) pObjects;

        return objects.get(r);
    }// w w  w.j  a  va 2 s. com
    return null;
}

From source file:com.shazam.fork.summary.OutcomeAggregator.java

private static Boolean and(final Collection<Boolean> booleans) {
    return BooleanUtils.and(booleans.toArray(new Boolean[booleans.size()]));
}

From source file:Main.java

/**
 * /*  w w w  .j  av a2 s  .c om*/
 * 
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> elementClass, int index, int count) {
    int n = collection.size();
    int end = Math.min(index + count, n);
    Object result = Array.newInstance(elementClass, end - index);
    Iterator<T> it = collection.iterator();
    int resultIndex = 0;
    for (int i = 0; i < end; i++) {
        if (!it.hasNext())
            break;
        Object value = it.next();
        if (i < index)
            continue;
        Array.set(result, resultIndex, value);
        resultIndex++;
    }
    return (T[]) result;
}

From source file:Main.java

/**
 * Returns an array of integer values from the set of integer values
 * @param set to return array for//from w  ww. j a  v  a  2 s. c  o  m
 * @return array
 */
public static int[] intArray(Collection<Integer> set) {
    if (set == null) {
        return new int[0];
    }
    int[] result = new int[set.size()];
    int index = 0;
    for (Integer value : set) {
        result[index++] = value;
    }
    return result;
}

From source file:Main.java

public static <T> T get(Collection<T> c, int index) {
    if (isEmpty(c))
        return null;

    if (index < 0 || index >= c.size())
        return null;

    if (c instanceof List)
        return (T) ((List<T>) c).get(index);

    List<? extends T> a = new ArrayList<T>(c);
    return a.get(index);
}

From source file:com.qwazr.utils.ArrayUtils.java

public static double[] toPrimitiveDouble(Collection<Double> collection) {
    double[] array = new double[collection.size()];
    int i = 0;//from  ww w .j ava 2  s  .co m
    for (double val : collection)
        array[i++] = val;
    return array;
}

From source file:Main.java

/**
 * Extract the lone element of the specified Collection.
 * @param <E> collection type/*from w  ww  .j  a  v  a  2s . co m*/
 * @param collection to read
 * @return sole member of collection
 * @throws IllegalArgumentException if collection is null/empty or contains more than one element
 * @since 4.0
 */
public static <E> E extractSingleton(final Collection<E> collection) {
    if (collection == null || collection.size() != 1) {
        throw new IllegalArgumentException("Can extract singleton only when collection size == 1");
    }
    return collection.iterator().next();
}