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

@SuppressWarnings({ "rawtypes" })
private static Object toFloatArray(Collection collection) {
    float[] fa = new float[collection.size()];

    int i = 0;//from   w  w w .jav a  2 s.  c o m
    for (Object o : collection) {
        float f = ((Float) o).floatValue();
        fa[i++] = f;
    }

    return fa;
}

From source file:Main.java

/**
 * Returns a copy of the given list that is safe to iterate over and perform actions that may
 * modify the original list./*  w  ww.java 2s  . c  o m*/
 *
 * <p> See #303 and #375. </p>
 */
public static <T> List<T> getSnapshot(Collection<T> other) {
    // toArray creates a new ArrayList internally and this way we can guarantee entries will not
    // be null. See #322.
    List<T> result = new ArrayList<T>(other.size());
    for (T item : other) {
        result.add(item);
    }
    return result;
}

From source file:org.socialhistoryservices.pid.util.NamingAuthority.java

public static List<String> getNaRole(Authentication userAuthentication) {

    final Collection<? extends GrantedAuthority> authorities = userAuthentication.getAuthorities();
    final List<String> nas = new ArrayList(authorities.size());
    for (GrantedAuthority authority : authorities) {
        String role = authority.getAuthority().replace("\n", ""); // ToDo: find out if there still is a \n in the role.
        if (role.startsWith(role_prefix)) {
            nas.add(role.substring(role_prefix.length()));
        } else if (role.startsWith(role_prefix_deprecated)) {
            nas.add(role.substring(role_prefix_deprecated.length()));
        }/*w w w .  j av  a  2  s  . c  o m*/
    }
    if (nas.size() == 0)
        throw new SecurityException("User " + userAuthentication.getName()
                + " has not got the required roles to use this service.");
    return nas;
}

From source file:com.cdancy.artifactory.rest.util.ArtifactoryUtils.java

public static String collectionToString(Collection<String> collection, String separator) {
    if (collection == null || collection.size() == 0)
        return null;
    Joiner joiner = Joiner.on(separator != null ? separator : ",").skipNulls();
    return joiner.join(collection);
}

From source file:Main.java

/**
 * Returns a String representation of this collection.
 * The items are converted to strings using String.format(frmtPattern).
 * If frmtPattern is given, the items will be formatted as
 * String.format(Object, frmtPattern), else it will use String.valueOf(Object).
 * If separatedby is given, the items will be separated by that string.
 *
 * @param c//w  ww .j av a2 s.  c om
 * @param separatedBy
 * @return
 */
public static String toString(Collection c, String separatedBy/*, String frmtPattern*/) {

    if (c == null || c.size() == 0)
        return "";

    //        boolean usePattern = frmtPattern != null;
    StringBuffer sb = new StringBuffer();
    for (Iterator itr = c.iterator(); itr.hasNext();) {
        Object o = itr.next();
        //            String s = usePattern ? String.format(frmtPattern, o) : String.valueOf(o);
        String s = String.valueOf(o);
        sb.append(s);
        if (separatedBy != null && itr.hasNext()) {
            sb.append(separatedBy);
        }
    }

    return sb.toString();
}

From source file:net.firejack.platform.core.utils.ArrayUtils.java

public static <T> T[] getArray(Collection<T> itemList, Class<T> clazz) {
    return itemList == null || itemList.isEmpty() ? null
            : itemList.toArray((T[]) Array.newInstance(clazz, itemList.size()));
}

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<>(segmentSize);
            for (int k = 0; k < segmentSize; k++) {
                segment.add(it.next());/*from  w w  w.  j a  v a 2  s.  com*/
            }
            arrays[i] = segment;
            start = stop;
            stop += segmentSize;
        }

        int segmentSize = set.size() - start;
        List<T> segment = new ArrayList<>(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

@SuppressWarnings({ "rawtypes" })
private static Object toBooleanArray(Collection collection) {
    boolean[] ba = new boolean[collection.size()];

    int i = 0;/*from w  w w  . j a va 2 s. co  m*/
    for (Object o : collection) {
        boolean b = ((Boolean) o).booleanValue();
        ba[i++] = b;
    }

    return ba;
}

From source file:com.flexive.shared.search.FxSQLFunctions.java

/**
 * Converts the given function names to {@link FxSQLFunction} objects. If an entry does not
 * represent a known FxSQL function, a FxRuntimeException is thrown.
 *
 * @param values    the function names to be converted
 * @return          the {@link FxSQLFunction} objects
 *///from w w  w  .ja  v a2s  .co m
public static List<FxSQLFunction> asFunctions(Collection<String> values) {
    final List<FxSQLFunction> result = new ArrayList<FxSQLFunction>(values.size());
    for (String value : values) {
        if (!FUNCTIONS.containsKey(value.toLowerCase())) {
            throw new FxNotFoundException("ex.sqlSearch.function.notFound", value,
                    StringUtils.join(getSqlNames(FUNCTIONS.values()), ", ")).asRuntimeException();
        }
        result.add(FUNCTIONS.get(value.toLowerCase()));
    }
    return result;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes" })
private static Object toDoubleArray(Collection collection) {
    double[] da = new double[collection.size()];

    int i = 0;/*  ww  w.j  a  va2  s  . c o  m*/
    for (Object o : collection) {
        double d;
        if (o instanceof Integer) {
            d = (Integer) o;
        } else {
            d = ((Double) o).doubleValue();
        }
        da[i++] = d;
    }

    return da;
}