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 <T> T[] toTypedArray(Collection<?> collection, Class<T> type) {
    @SuppressWarnings("unchecked")
    T[] array = (T[]) Array.newInstance(type, collection.size());
    Object[] data = collection.toArray();
    for (int i = 0; i < array.length; i++) {
        array[i] = type.cast(data[i]);/*from  w  ww.  ja  va  2s . c  om*/
    }
    return array;

}

From source file:Main.java

/**
 * Convert collection to array. This is a bit cleaner version of
 * {@link Collection#toArray(Object[])}.
 *//*from w ww  . j av a2 s.  c o  m*/
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<? extends T> collection, Class<T> type) {
    T[] result = (T[]) java.lang.reflect.Array.newInstance(type, collection.size());

    Iterator<? extends T> it = collection.iterator();
    for (int i = 0; i < collection.size(); i++) {
        result[i] = it.next();
    }

    return result;
}

From source file:Main.java

/**
 * Returns an array containing all of the elements of the
 * Iterator//from   w w  w  .  jav  a  2s .  c  om
 * @param iterator Iterator to copy the contexts of
 * @return an array containing a copy of the iterator contents
 */
public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) {
    if (iterator.hasNext()) {
        Collection arrayList = arrayList(iterator);
        T[] outArray = (T[]) Array.newInstance(type, arrayList.size());

        return (T[]) arrayList.toArray(outArray);
    } else {
        // optimize empty iterator case
        return (T[]) Array.newInstance(type, 0);
    }
}

From source file:Main.java

/**
 * Enforce that a Collection contains no more than one element and return that element
 * if it exists.//from   www  . j a v a  2 s .c om
 * 
 * @param collection the Collection to examine
 * @return null if the Collection is null or empty, the Collection's single object if
 *       its size is one, or throw if its size is greater than one.
 * @throws IllegalArgumentException if the Collection contains more than one element
 */
public static <T> T singleObject(Collection<T> collection) {
    if (collection == null || collection.isEmpty())
        return null;
    if (collection.size() > 1)
        throw new IllegalArgumentException("more than one element in collection");
    return collection.iterator().next();
}

From source file:org.springframework.cloud.vault.VaultErrorMessage.java

/**
 * Obtain the error message from a JSON response.
 * /*from   ww  w . ja v  a 2 s  . c o m*/
 * @param json
 * @return
 */
static String getError(String json) {

    if (json.contains("\"errors\":")) {

        try {
            Map<String, Object> map = OBJECT_MAPPER.readValue(json.getBytes(), Map.class);
            if (map.containsKey("errors")) {

                Collection<String> errors = (Collection<String>) map.get("errors");
                if (errors.size() == 1) {
                    return errors.iterator().next();
                }
                return errors.toString();
            }

        } catch (IOException o_O) {
            // ignore
        }
    }
    return json;
}

From source file:Main.java

public static <T> List<T> appendAllList(List<T> list, Collection<? extends T> c) {
    if (list == null) {
        list = new ArrayList<T>(1);
    }/*w ww. ja va2 s .  c  o m*/
    if (c != null && c.size() > 0) {
        list.addAll(c);
    }
    return list;
}

From source file:Main.java

/**
 * Ensures that all elements of the given collection can be cast to a desired type.
 * /*from ww  w  . j a  v  a2s .  c o m*/
 * @param collection the collection to check
 * @param type the desired type
 * @return a collection of the desired type
 * @throws ClassCastException if an element cannot be cast to the desired type
 */
@SuppressWarnings("unchecked")
public static <E> Collection<E> checkCollection(Collection<?> collection, Class<E> type) {
    if (DEBUG) {
        Collection<E> copy = new ArrayList<E>(collection.size());
        for (Object element : collection) {
            copy.add(type.cast(element));
        }
        return copy;
    }
    return (Collection<E>) collection;
}

From source file:Main.java

public static long[] toPrimitive(Collection<Long> collection) {
    if (isEmpty(collection)) {
        return new long[0];
    }/*from   w  w  w.j  ava 2s  .  com*/

    long[] array = new long[collection.size()];
    int i = 0;
    for (Long val : collection) {
        array[i] = val;
        i++;
    }
    return array;
}

From source file:Main.java

public static <T extends Object> int countIntersection(Collection<? extends T> s1, Collection<? extends T> s2) {
    if (s1 == null || s2 == null)
        return 0;
    int n1 = s1.size();
    int n2 = s2.size();
    if (n2 < n1) {
        Collection<? extends T> t = s1;
        s1 = s2;//from   w w w  .  j ava2 s.  com
        s2 = t;
    }
    int n = 0;
    for (T e : s1) {
        if (s2.contains(e))
            n++;
    }
    return n;
}

From source file:Main.java

/**
 * Converts the <code>collection</code> into an array
 * using the <code>generator</code> to create the array
 * without creating a <code>Stream</code> on the collection.
 *
 * @param collection The collection to be converted
 * @param generator The generator to create the empty array
 * @param <T> The type of the elements
 *
 * @return An array with the length of the <code>collection</code>
 * containing all elements of the <code>collection</code>
 *
 * @throws NullPointerException if either <code>collection</code>
 * or <code>generator</code> is <code>null</code>
 *///from w  w w . j  a  va 2 s. co  m
public static <T> T[] toArray(Collection<T> collection, IntFunction<T[]> generator)
        throws NullPointerException {
    return collection.toArray(generator.apply(collection.size()));
}