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 int[] toIntArray(Collection<Integer> items) {
    int ret[] = new int[items.size()];
    int idx = 0;/*from w  ww  .  ja  v  a  2  s.  c o m*/
    for (Integer i : items) {
        assert (i != null);
        ret[idx++] = i.intValue();
    } // FOR
    return (ret);
}

From source file:Main.java

public static int[] toIntArray(Collection<? extends Number> c) {
    int arr[] = new int[c.size()];
    int i = 0;//from  ww  w. j  a  va2s.  c  o m
    for (Number item : c)
        arr[i++] = item.intValue();
    return arr;
}

From source file:Utils.java

public static <T> Collection<T> diff(Collection<T> c1, Collection<T> c2) {
    if (c1 == null || c1.size() == 0 || c2 == null || c2.size() == 0) {
        return c1;
    }/*from  w w w  . j ava  2  s.  c o  m*/
    Collection<T> difference = new ArrayList<T>();
    for (T item : c1) {
        if (!c2.contains(item)) {
            difference.add(item);
        }
    }
    return difference;
}

From source file:Main.java

public static <T> List<T> cons(T x, Collection<? extends T> xs) {
    ArrayList<T> result = new ArrayList<>(xs.size() + 1);
    result.add(x);//from ww w .j  a  v a 2s  . co m
    result.addAll(xs);
    return result;
}

From source file:Main.java

/**
 * Returns a slice of a collection, up to maxLen in size
 *
 * @param c      the collection to cut//from ww  w.  j  a  va2 s . c o  m
 * @param maxLen the length of the slice
 * @return a slice of {@code c} {@code maxLen} in size, if {@code maxLen} is less than {@code c.size()}, otherwise, return {@code c}
 */
public static <E> Collection<E> cutMaxLen(Collection<E> c, int maxLen) {
    if (maxLen < c.size()) {
        return new ArrayList<>(c).subList(0, maxLen);
    }
    return c;
}

From source file:Main.java

public static List<String> toStrings(Collection<?> c) {
    List<String> result = new ArrayList<String>(c.size());
    for (Object e : c) {
        result.add(e.toString());// ww w . j  a  v  a  2  s. co m
    }
    return result;
}

From source file:Main.java

public static final long[] collection2longArray(Collection<Object> collection) {
    long[] array = new long[collection.size()];
    int index = 0;
    for (Object value : collection) {
        try {//from  ww w. j av  a 2s . co m
            array[index] = Long.parseLong(String.valueOf(value));
            index++;
        } catch (Exception e) {
        }
    }
    return array;
}

From source file:Main.java

/**
 * Judge whether a collection is null or size is 0
 *
 * @param c/*from w w  w.  j av  a 2 s .c  o  m*/
 * @param <V>
 * @return
 */
public static <V> boolean isEmpty(Collection<V> c) {
    return (c == null || c.size() == 0);
}

From source file:Main.java

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E fromLast(Collection<E> collection, int fromLast) {
    return (E) collection.toArray()[collection.size() - 1 - fromLast];
}