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

/**
 * Converts the given collection to a collection of strings by calling toString() on each
 * item.//from ww  w .j  av a  2  s  . c o  m
 *
 * @param collection    The collection to convert.
 * @param includeNulls  Whether null items should be included in the returned collection.
 * @param <E>           The type of item held by the given collection.
 * @return              A collection of the given items converted to strings.
 */
public static <E> Collection<String> stringify(Collection<E> collection, boolean includeNulls) {
    if (collection == null)
        return null;

    List<String> output = new ArrayList<String>(collection.size());

    for (E item : collection) {
        if (includeNulls || item != null) {
            output.add(item == null ? null : item.toString());
        }
    }

    return output;
}

From source file:com.cloudera.oryx.common.OryxTest.java

private static <T> String abbreviatedToString(Collection<T> c) {
    return c.size() <= 16 ? c.toString() : c.stream().limit(16).collect(Collectors.toList()) + "...";
}

From source file:Main.java

/**
 * @since 1.5//from   w  w w .ja va2 s.  com
 */
public static String toString(final Collection<?> c, final String sep) {
    if (c instanceof List) {
        return toString((List<?>) c, sep);
    }
    final int n = c.size();
    if (n <= 0) {
        return ""; //$NON-NLS-1$
    } else if (n == 1) {
        return c.iterator().next().toString();
    } else {
        final Iterator<?> iter = c.iterator();
        final StringBuilder sb = new StringBuilder(iter.next().toString());
        for (int i = 1; i < n; i++) {
            sb.append(sep);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }
}

From source file:Main.java

public static boolean containsAll(Collection a, Collection b) {
    // fast paths
    if (a == b)/* w w w  .  j  av a  2 s . c  o  m*/
        return true;
    if (b.size() == 0)
        return true;
    if (a.size() < b.size())
        return false;

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                    ao = (Comparable) ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else {
                    return false;
                }
            }
        } else if (bbc.equals(aac)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = bi.next();
                    ao = ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else {
                    return false;
                }
            }
        }
    }
    return a.containsAll(b);
}

From source file:de.drippinger.serviceExtractor.files.FileSearch.java

/**
 * Find all ndf files.// w w  w .  j a  v a 2 s.c  o  m
 *
 * @param parameter the run parameter
 * @return a collection of ndf files
 */
public static Collection<File> findNdfFiles(@NonNull RunParameter parameter) {
    File rootFolder = new File(parameter.pathToFlows());

    Collection<File> files = FileUtils.listFiles(rootFolder, FileFilterUtils.suffixFileFilter("ndf"),
            TrueFileFilter.INSTANCE);

    log.debug("Found {} NDF files", files.size());

    return files;
}

From source file:com.chrischurchwell.jukeit.server.ServerHandler.java

public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
    List<T> r = new ArrayList<T>(c.size());
    for (Object o : c)
        r.add(clazz.cast(o));//w w  w.ja v a 2s.c  o m
    return r;
}

From source file:com.skyisland.questmanager.fanciful.ArrayWrapper.java

/**
 * Converts an iterable element collection to an array of elements.
 * The iteration order of the specified object will be used as the array element order.
 * @param list The iterable of objects which will be converted to an array.
 * @param c The type of the elements of the array.
 * @return An array of elements in the specified iterable.
 *///from   w w w .j  ava2 s  .co m
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Iterable<? extends T> list, Class<T> c) {
    int size = -1;
    if (list instanceof Collection<?>) {
        @SuppressWarnings("rawtypes")
        Collection coll = (Collection) list;
        size = coll.size();
    }

    if (size < 0) {
        size = 0;
        // Ugly hack: Count it ourselves
        for (@SuppressWarnings("unused")
        T element : list) {
            size++;
        }
    }

    T[] result = (T[]) Array.newInstance(c, size);
    int i = 0;
    for (T element : list) { // Assumes iteration order is consistent
        result[i++] = element; // Assign array element at index THEN increment counter
    }
    return result;
}

From source file:Main.java

/**
 * Split the collection into weights.length
 *
 * @param set     the colleciton to split
 * @param weights is an array of weights that will be normalized, so sum can be larger than one, but all should be positive
 * @return an array of lists each containing a number of objects from the collection, the number determined by the weight relative to total weight
 *//*from w w  w. ja v a2  s .com*/
public static <T> List<T>[] split(Collection<T> set, double[] weights) {
    if (set.size() >= weights.length) {
        @SuppressWarnings("unchecked")
        List<T>[] arrays = new List[weights.length];

        double totalweight = 0.0;
        for (double w : weights)
            totalweight += w;
        double[] relativeWeight = new double[weights.length];
        for (int i = 0; i < weights.length; i++)
            relativeWeight[i] = weights[i] / totalweight;
        int[] listSizes = new int[weights.length];
        for (int i = 0; i < weights.length; i++)
            listSizes[i] = (int) (weights[i] / totalweight * (double) set.size());
        int allocated = 0;
        for (int ls : listSizes)
            allocated += ls;
        int remainder = set.size() - allocated;

        listSizes[0] += remainder;

        Iterator<T> it = set.iterator();
        for (int i = 0; i < listSizes.length; i++) {
            List<T> segment = new ArrayList<>(listSizes[i]);
            for (int k = 0; k < listSizes[i]; k++) {
                segment.add(it.next());
            }
            arrays[i] = segment;
        }
        return arrays;
    } else {
        throw new IllegalArgumentException("weigths.length must not be smaller than set size!");
    }
}

From source file:com.aw.core.db.support.WhereBuilder2.java

public static String buildIn(Collection idList, boolean removeNulls) {
    if (removeNulls) {
        List idListTrim = new ArrayList(idList.size());
        for (Object id : idList) {
            if (id != null)
                idListTrim.add(id);//from   w w w  . ja va2  s  .  c  o  m
        }
        idList = idListTrim;
    }
    if (idList.size() == 0)
        return null;

    boolean isNumber = idList.iterator().next() instanceof Number;
    StringBuffer result = isNumber ? ListUtils.concatenarSepPorComa(idList)
            : ListUtils.concatenarSepPorComaYApostrofe(idList);
    return result.toString();
}