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 sizeOf(Collection<?> collection) {
    if (isEmpty(collection)) {
        return 0;
    }/*from   w ww  . j  ava2  s. c  o m*/
    return collection.size();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <S, D extends S> D[] downcastToArray(Collection<S> source, Class<D> destClass) {
    D[] dest = (D[]) Array.newInstance(destClass, source.size());
    Iterator<S> it = source.iterator();
    for (int i = 0; i < dest.length; i++) {
        S s = it.next();//ww w  .  ja  v  a  2 s.c  om
        if (destClass.isAssignableFrom(s.getClass()))
            dest[i] = (D) s;
        else
            throw new IllegalArgumentException("Could not downcast element from class "
                    + s.getClass().getSimpleName() + " to " + destClass.getSimpleName());
    }
    return dest;
}

From source file:Main.java

public static <T> boolean isOutOfBound(Collection<T> coll, int index) {
    if (isEmpty(coll)) {
        return true;
    }/*from   w  w w  .  j a  va 2 s .  co m*/
    if ((index >= coll.size()) || (index < 0)) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Null safe summation of the sizes of all of the given collections
 *//*  w  w w.  j  a va 2 s .  co  m*/
public static int size(Collection... collections) {
    int size = 0;
    if (collections != null) {
        for (Collection collection : collections) {
            if (collection != null)
                size += collection.size();
        }
    }
    return size;
}

From source file:uk.co.blackpepper.support.spring.jdbc.jooq.SpringJdbcJooqUtils.java

@VisibleForTesting
static int[] getSqlTypes(Collection<? extends Param<?>> params) {
    List<Integer> types = new ArrayList<>(params.size());

    for (Param<?> param : params) {
        types.add(javaTypeToSqlParameterType(param.getType()));
    }//  www .j  a va  2  s . c o m

    return Ints.toArray(types);
}

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 java.lang.String) {
                ret += separator + (String) x;
            } else {
                ret += separator + x.toString();
            }/*www .j  a va2s . c  om*/
        }
    }
    return ret.replaceFirst(separator, "");

}

From source file:Main.java

/**
 * @return The intersection of two sets A and B is the set of elements common to A and B
 *//*from  w w w  .j ava2 s.c  o m*/
public static <T> List<T> setIntersection(Collection<T> listA, Collection<T> listB) {
    ArrayList<T> intersection = new ArrayList<T>(listA.size());

    for (T obj : listA) {
        if (listB.contains(obj)) {
            intersection.add(obj);
        }
    }
    return intersection;
}

From source file:Main.java

public static String collectionToString(Collection c) {
    StringBuffer buff = new StringBuffer();
    int i = 0;//from  w  w  w. j  av  a2 s  .c o  m
    int size = c.size();
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        Object object = iter.next();
        if (size > 1 && i == (size - 1)) {
            buff.append("and ");
        }
        buff.append(object);
        if (i < (size - 1)) {
            if (size > 2) {
                buff.append(", ");
            } else {
                buff.append(" ");
            }
        }
        i++;
    }
    return buff.toString();

}

From source file:Main.java

/**
 * @param <T>//from w ww.  j  a  va 2s . c o  m
 * @param col
 * @return a list without null elements
 */
public static <T> ArrayList<T> clearNulls(Collection<T> col) {
    ArrayList<T> list = new ArrayList<T>(col.size());

    for (T t : col) {
        if (t != null) {
            list.add(t);
        }
    }

    return list;
}

From source file:Main.java

/**
 * Put all of the elements in items into the given array
 * This assumes that the array has been pre-allocated
 * @param <T>/*from   www  .  j a  va 2  s . com*/
 * @param items
 * @param array
 */
public static <T> void toArray(Collection<T> items, Object array[], boolean convert_to_primitive) {
    assert (items.size() == array.length);

    int i = 0;
    for (T t : items) {
        if (convert_to_primitive) {
            if (t instanceof Long) {
                array[i] = ((Long) t).longValue();
            } else if (t instanceof Integer) {
                array[i] = ((Integer) t).intValue();
            } else if (t instanceof Double) {
                array[i] = ((Double) t).doubleValue();
            } else if (t instanceof Boolean) {
                array[i] = ((Boolean) t).booleanValue();
            }
        } else {
            array[i] = t;
        }
    }
    return;
}