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 String toString(Collection<?> collection) {
    StringBuilder sb = new StringBuilder(collection.size() * 2);

    for (Object item : collection) {
        sb.append(item).append(",");
    }//from   www .  j a v a2  s  .  co  m
    // sb.deleteCharAt(sb.lastIndexOf(","));
    return sb.toString();
}

From source file:Main.java

/**
 * Converts a collection to a list by either casting or creating a concrete
 * list depending on the type of collection given.
 * @param <T> The type of the elements in the collection.
 * @param c the collection. If <code>null</code>, <code>null</code> is
 *        returned.//from w  w  w. j  av  a  2  s  .c o m
 * @return {@link List} containing the same elements as the given
 *         {@link Collection} param.
 */
public static <T> List<T> listFromCollection(Collection<T> c) {
    if (c == null)
        return null;
    if (c.size() < 1) {
        return new ArrayList<T>(0);
    }
    return (c instanceof List<?>) ? (List<T>) c : new ArrayList<T>(c);
}

From source file:Main.java

public static int[] toIntArray(Collection<Integer> ints) {

    final int[] result = new int[ints.size()];
    int upto = 0;
    for (int v : ints) {
        result[upto++] = v;/*  w  w w  .j  a v  a2s  . c o m*/
    }

    // paranoia:
    assert upto == result.length;

    return result;
}

From source file:Main.java

/**
 * Determines the size of the specified Collection.  If the Collection is null or contains no elements, then the
 * size of the Collection is 0, otherwise the size of the Collection is determined by it's size() method.
 * //from   w  ww.j  ava2 s .  c om
 * @param collection the Collection who's size is being determined.
 * @return an integer value specifying the size of the Collection.
 * @see java.util.Collection#size()
 */
public static int size(final Collection collection) {
    return (collection == null ? 0 : collection.size());
}

From source file:Main.java

/**
 * Convert a collection to an array//  w  ww  .  jav  a  2s . c  om
 * 
 * @param col
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> col, Class<? super T> c) {
    int size = col.size();
    T[] array = (T[]) Array.newInstance(c, size);
    col.toArray(array);

    return array;
}

From source file:Main.java

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

From source file:Main.java

public static <T> boolean contains(T elem, Collection<T> collection) {
    if (collection != null && collection.size() > 0) {
        for (T ds : collection) {
            if (elem.equals(ds)) {
                return true;
            }/*from   w ww  . jav a 2 s.  co  m*/
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns true if the collection is null or 0-length.
 *
 * @param collection the collection to be examined
 * @return true if str collection null or zero length
 *///from w ww  .  ja  v  a2 s .c  om
public static boolean isEmpty(Collection<?> collection) {
    if (collection == null || collection.isEmpty() || collection.size() == 0) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Return <code>true</code> if the supplied Collection is not <code>null</code>
 * and contain only one element. Otherwise, return <code>false</code>.<br>
 *
 * @param collection the Collection to be checked
 * @return whether the given Collection is Single
 *///  w w  w. java2s.c  o m
public static boolean isSingle(Collection<?> collection) {
    return (collection != null && collection.size() == 1);
}

From source file:Main.java

/**
 * Validate given collection argument isn't null or empty string.
 *
 * @param arg argument to validate// w  ww. ja  v  a  2 s  .c om
 * @param argName name of the argument to show in error message
 * @throws IllegalArgumentException
 */
public static <T> void notNullOrEmpty(Collection<T> arg, String argName) {
    if (arg == null || arg.size() < 1) {
        throw new IllegalArgumentException("argument is null: " + argName);
    }
}