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

From source file:Main.java

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

From source file:Main.java

public static String collectionToSlashString(Collection<?> collection) {
    if (collection != null && collection.size() > 0) {
        StringBuilder sb = new StringBuilder();
        Object obj = null;/* w  w w  .  ja v  a  2  s .  c  om*/
        for (Iterator<?> iterator = collection.iterator(); iterator.hasNext();) {
            obj = iterator.next();
            sb.append(obj.toString());
            if (iterator.hasNext()) {
                sb.append("/");
            }
        }
        return sb.toString();
    } else {
        return "";
    }
}

From source file:Main.java

private static <E> boolean fallBackToStandardImplementation(final Collection<E> coll, final int limit) {
    return limit >= coll.size() || limit < 0 || coll.isEmpty();
}

From source file:Main.java

/**
 * Modify the specified Collection so that only the first <code>limit</code> elements
 * remain, as determined by iteration order. If the Collection is smaller than limit,
 * it is unmodified.//from   w w w.  j a  v  a2s.c  o  m
 */
public static void limit(Collection<?> col, int limit) {
    int size = col.size();
    if (size > limit) {
        if (col instanceof List<?>) {
            ((List<?>) col).subList(limit, size).clear();

        } else {
            Iterator<?> itr = col.iterator();
            for (int ii = 0; ii < limit; ii++) {
                itr.next();
            }
            while (itr.hasNext()) {
                itr.next();
                itr.remove();
            }
        }
    }
}

From source file:Main.java

public static <T> boolean collectionEquals(Collection<T> col1, Collection<T> col2) {
    if (col1.size() != col2.size()) {
        return false;
    }/*from   w  ww  .ja  va  2s  .  c  o  m*/

    for (T item : col1) {
        if (!col2.contains(item)) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static long[] toArray(Collection<Long> collection) {
    long[] array = new long[collection.size()];
    Iterator<Long> it = collection.iterator();
    int i = 0;//ww  w . j  a v  a  2 s .co  m
    while (it.hasNext()) {
        array[i++] = it.next();
    }
    return array;
}

From source file:Main.java

/**
 * Create new {@link HashMap} having same size of given base collection.
 *
 * @param base collection which size will be referred
 * @param <K>  keyType// w  w w.ja  v  a2 s.  c o  m
 * @param <V>  valueType
 * @return created {@link HashMap} instance
 */
public static <K, V> HashMap<K, V> newHashMap(Collection<?> base) {
    return new HashMap<K, V>(base.size());
}

From source file:Main.java

public static Boolean isEmpty(Collection<?> collection) {
    if (collection == null || collection.size() < 1) {
        return Boolean.TRUE;
    }//from   www.  j a  v  a2s . c  om
    return Boolean.FALSE;
}

From source file:Main.java

public static <T extends Object> T getFirst(Collection<T> items, T otherwise) {
    if (items == null || items.size() == 0)
        return otherwise;
    return items.iterator().next();
}