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

/**
 * Calls the {@link Collection#toArray(Object[])} method constructing array based on the given argument type.
 *
 * @param <E>/*from  w  w  w  .j av a 2 s . co  m*/
 *            the element type
 * @param collection
 *            the collection
 * @param type
 *            the type
 * @return the e[]
 */
@SuppressWarnings("unchecked")
public static <E> E[] toArray(Collection<? extends E> collection, Class<E> type) {
    return collection.toArray((E[]) Array.newInstance(type, collection.size()));
}

From source file:Main.java

public static <ELEMENT> LinkedHashSet<ELEMENT> newLinkedHashSet(Collection<ELEMENT> elements) {
    final LinkedHashSet<ELEMENT> set = newLinkedHashSetSized(elements.size());
    set.addAll(elements);/*from w  w w  .j  av a2 s  . c om*/
    return set;
}

From source file:Main.java

/**
 * @param list/*w  w  w  . j av  a 2  s. c om*/
 * @return the size of the collection or 0 if the collection is null
 */
public static int getSize(Collection<?> collection) {

    if (collection == null) {

        return 0;
    }

    return collection.size();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static int sizeOf(Collection collection) {
    if (isEmpty(collection)) {
        return 0;
    }//from  w  w w .  j  a  va 2  s.c o m
    return collection.size();
}

From source file:Main.java

/**
 * Returns the size of the collection if it can be determined to be a collection
 *
 * @param value the collection//  w w w . j  ava  2s.  c  om
 * @return the size, or <tt>null</tt> if not a collection
 */
public static Integer size(Object value) {
    if (value != null) {
        if (value instanceof Collection) {
            Collection<?> collection = (Collection<?>) value;
            return collection.size();
        } else if (value instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) value;
            return map.size();
        } else if (value instanceof Object[]) {
            Object[] array = (Object[]) value;
            return array.length;
        } else if (value.getClass().isArray()) {
            return Array.getLength(value);
        } else if (value instanceof NodeList) {
            NodeList nodeList = (NodeList) value;
            return nodeList.getLength();
        }
    }
    return null;
}

From source file:Main.java

/************************ size ************************************/

public static <T> int nullSafeSize(Collection<T> collection) {
    if (collection == null) {
        return 0;
    }//from www  .  j ava  2s.c o  m
    return collection.size();
}

From source file:Main.java

/**
 * To Array//from  w w w . j  ava2s. co m
 * 
 * @param c
 *            Collection
 * @return T[]
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> c) {
    if (c == null) {
        return null;
    }
    return (T[]) c.toArray(new Object[c.size()]);
}

From source file:Main.java

/**
 * Traverse collection//from   ww w . jav  a2 s  . c om
 *
 * @param c
 * @param <T>
 * @return
 */
public static <T> String traverseCollection(Collection<T> c) {
    if (!isEmpty(c)) {
        int len = c.size();
        StringBuilder builder = new StringBuilder(len);
        int i = 0;
        for (T t : c) {
            if (t == null) {
                continue;
            }
            builder.append(t.toString());
            i++;
            if (i < len) {
                builder.append(DELIMITER);
            }
        }
        return builder.toString();
    }
    return null;
}

From source file:Main.java

public static <T> int size(Collection<T> list) {
    if (list == null) {
        return 0;
    }/*from  w ww .  j  a v a  2s. c om*/
    return list.size();
}

From source file:Main.java

public static <T> String join(Collection<T> col, String separator) {
    String ret = "";
    if (col != null && col.size() > 0) {
        for (Object x : col) {
            if (x instanceof String) {
                ret += separator + (String) x;
            } else {
                ret += separator + x.toString();
            }//from w  w w  .j  a v a2s.com
        }
    }
    return ret.replaceFirst(separator, "");

}