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> void resize(Collection<T> collection, int size, T value) {
    if (collection == null)
        return;/* w  ww  .  j  a v  a 2  s  . co m*/

    if (collection.size() < size) {
        //collection.addAll
    }
}

From source file:Main.java

public static int sizeOf(Collection collection) {
    if (collection == null) {
        return 0;
    } else {/*from w w w.  j  av a 2  s . c  o  m*/
        return collection.size();
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] collectionToArray(Collection<T> sourceCollection, Class<?> elementClass) {
    Object array = Array.newInstance(elementClass, sourceCollection.size());
    Iterator iterator = sourceCollection.iterator();
    for (int i = 0; i < sourceCollection.size(); i++) {
        Array.set(array, i, iterator.next());
    }/* ww w.ja v a 2s.  c o m*/
    return (T[]) array;
}

From source file:Main.java

/**
 * Answers true if a predicate is true for at least one element of a
 * collection./*from www .  jav a 2 s  .c om*/
 *
 * @param <T>
 * @param collection - the collection to get the input from, may be null
 * @param value      - value to check
 * @return true if a collection contains specified value.
 */
public static <T> boolean contains(Collection<T> collection, T value) {
    return collection != null && collection.size() > 0 && collection.contains(value);
}

From source file:Main.java

/**
 * Converts collection to a string array by calling toString on each member.
 *
 * @param collection collection to convert
 * @return string array/*from  w  w w .  j  a  v  a 2s. c  o  m*/
 */
public static String[] toStringArray(Collection collection) {
    String[] stringArray = new String[collection.size()];

    int i = 0;
    for (Object string : collection) {
        stringArray[i++] = string.toString();
    }

    return stringArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] toArray(final Collection<T> c, final Class<? extends T[]> newType) {
    final int length = c.size();
    final T[] array = (T[]) Array.newInstance(newType.getComponentType(), length);
    if (length > 0) {
        int i = 0;
        for (T elem : c) {
            array[i++] = elem;//ww  w. j  a v a2s .c o  m
        }
    }
    return array;
}

From source file:Main.java

public static <ELEMENT> ArrayList<ELEMENT> newArrayList(Collection<ELEMENT> elements) {
    final ArrayList<ELEMENT> list = newArrayListSized(elements.size());
    list.addAll(elements);/*from   w  w  w .j  av  a 2 s.  co  m*/
    return list;
}

From source file:Main.java

public static <T> List<T>[] split(Collection<T> set, int n) {
    if (set.size() >= n) {
        @SuppressWarnings("unchecked")
        List<T>[] arrays = new List[n];
        int minSegmentSize = (int) Math.floor(set.size() / (double) n);

        int start = 0;
        int stop = minSegmentSize;

        Iterator<T> it = set.iterator();

        for (int i = 0; i < n - 1; i++) {
            int segmentSize = stop - start;
            List<T> segment = new ArrayList<T>(segmentSize);
            for (int k = 0; k < segmentSize; k++) {
                segment.add(it.next());/*w w  w  .j av  a 2s.  c om*/
            }
            arrays[i] = segment;
            start = stop;
            stop += segmentSize;
        }

        int segmentSize = set.size() - start;
        List<T> segment = new ArrayList<T>(segmentSize);
        for (int k = 0; k < segmentSize; k++) {
            segment.add(it.next());
        }
        arrays[n - 1] = segment;

        return arrays;
    } else {
        throw new IllegalArgumentException("n must not be smaller set size!");
    }
}

From source file:Main.java

/**
 * Answers true if a predicate is true for at least one element of a
 * collection.//from  w w  w  .j  a v a 2 s . co  m
 * 
 * @param <T>
 * @param collection
 *            - the collection to get the input from, may be null
 * @param value
 *            - value to check
 * @return true if a collection contains specified value.
 */
public static <T> boolean contains(Collection<T> collection, T value) {
    return collection != null && collection.size() > 0 ? collection.contains(value) : false;
}

From source file:Main.java

public static String getShorty(Collection<? extends CharSequence> params, String returnType) {
    StringBuilder sb = new StringBuilder(params.size() + 1);
    sb.append(getShortyType(returnType));
    for (CharSequence typeRef : params) {
        sb.append(getShortyType(typeRef));
    }/*w w  w . j  a v  a2 s  .  c o  m*/
    return sb.toString();
}