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

@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> c, Class<T> clazz) {
    if (c == null || c.size() == 0) {
        T[] a = (T[]) java.lang.reflect.Array.newInstance(clazz, 0);
        return Collections.<T>emptyList().toArray(a);
    } else {//  w  w w  .jav  a 2s.c  om
        T[] a = (T[]) java.lang.reflect.Array.newInstance(clazz, c.size());
        return c.toArray(a);
    }
}

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 *///from w ww.j  a v a  2s .  co  m
private static <T> T getElement(final boolean remove, final int index, final Collection<T> coll) {
    if (index >= coll.size())
        throw new IndexOutOfBoundsException(index + " >= " + coll.size());

    final Iterator<T> it = coll.iterator();
    int i = 0;

    while (i++ < index)
        it.next();

    final T elem = it.next();
    if (remove)
        it.remove();
    return elem;
}

From source file:Main.java

public static int[] toIntArray(Collection<Integer> integerCollection) {
    int[] intArray = new int[integerCollection.size()];
    int index = 0;

    for (int num : integerCollection) {
        intArray[index] = num;/*  w  w w.j av a 2s .c  o m*/
        index++;
    }

    return intArray;
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

/**
 * Copies a collection into an array./*from w  ww .  j a  v  a  2s  . co m*/
 * 
 * @param collection
 *        is the collection that should be copied to an array
 * @param type
 *        is the class of the elements in the collection
 * @return the array copy
 */
public static <T> T[] toArray(final Collection<T> collection, final Class<T> type) {

    final int size = collection.size();
    @SuppressWarnings("unchecked")
    final T[] array = (T[]) Array.newInstance(type, size);
    collection.toArray(array);

    return array;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] asArray(final Collection<? extends E> collection) {
    return collection.toArray((E[]) new Object[collection.size()]);
}

From source file:Main.java

public static <K, V> Map<K, V> convertMap(final Map<K, V> map, final Collection<K> keys,
        final Collection<V> values) {
    if (keys.size() != values.size()) {
        throw new IllegalArgumentException("Invalid Collection. Collection size is not equals.");
    }/*w ww  . j  a v a  2s  .c o  m*/
    Iterator<V> valueIt = values.iterator();
    for (K key : keys) {
        map.put(key, valueIt.next());
    }
    return map;
}

From source file:Main.java

public static <T> boolean addAll(Collection<T> collection, Collection<? extends T> toAdd) {
    int size = toAdd.size();
    boolean result = false;
    if (size > 0) {
        if (size < 10)
            for (T element : toAdd)
                result |= collection.add(element);
        else//www  .  ja v  a  2  s. c  o m
            result = collection.addAll(toAdd);
    }
    return result;
}

From source file:Main.java

public static <T> boolean isNull(Collection<T> collection) {
    boolean isNull = true;
    if (collection != null && collection.size() != 0) {
        isNull = false;/*from  ww w  .ja va 2 s  .c o m*/
    }
    return isNull;
}