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:com.micmiu.modules.utils.Collections3.java

/**
 * ????(Getter), ??List.//from w  w  w. ja  v  a2s  .  com
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */

public static List extractToList(final Collection collection, final String propertyName) {
    List list = new ArrayList(collection.size());

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw Reflections.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:com.enonic.cms.core.content.ContentKey.java

public static List<ContentKey> convertToList(Collection<Integer> collection) {
    if (collection == null || collection.size() == 0) {
        return null;
    }/*from  www  .j a  v  a2 s .  c om*/

    List<ContentKey> list = new ArrayList<ContentKey>(collection.size());
    for (int value : collection) {
        list.add(new ContentKey(value));

    }
    return list;
}

From source file:Main.java

/**
 * A version of {@link #zipMap(Object[], Object[])} that works with 
 * {@link Collection}s.//from   w  ww.j av a2 s. c o  m
 * 
 * @param keys Items that will be the keys in the resulting {@code Map}.
 * @param values Items that will be the values in the result {@code Map}.
 * 
 * @return A {@code Map} whose entries are of the form 
 * {@code keys[N], values[N]}.
 * 
 * @see #zipMap(Object[], Object[])
 */
public static <K, V> Map<K, V> zipMap(Collection<? extends K> keys, Collection<? extends V> values) {
    Map<K, V> zipped = new LinkedHashMap<>(keys.size());
    Iterator<? extends K> keyIterator = keys.iterator();
    Iterator<? extends V> valueIterator = values.iterator();
    while (keyIterator.hasNext() && valueIterator.hasNext()) {
        zipped.put(keyIterator.next(), valueIterator.next());
    }
    return zipped;
}

From source file:legendarena.api.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  ww  w.  j a v  a 2  s .  c om*/
@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

/**
 * Returns <code>true</code> iff at least one element is in both collections15.
 * <p/>/*from   w w  w.  j  av a2  s  .c om*/
 * In other words, this method returns <code>true</code> iff the
 * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
 *
 * @param coll1 the first collection, must not be null
 * @param coll2 the first collection, must not be null
 * @return <code>true</code> iff the intersection of the collections15 is non-empty
 * @see #intersection
 * @since 2.1
 */
public static <E> boolean containsAny(final Collection<? extends E> coll1,
        final Collection<? extends E> coll2) {
    if (coll1.size() < coll2.size()) {
        for (Iterator it = coll1.iterator(); it.hasNext();) {
            if (coll2.contains(it.next())) {
                return true;
            }
        }
    } else {
        for (Iterator it = coll2.iterator(); it.hasNext();) {
            if (coll1.contains(it.next())) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static List<String> getSortedStringList(Collection<String> toSort) {
    List<String> sorted = new LinkedList<String>();
    final PriorityQueue<String> ordered = new PriorityQueue<String>(
            toSort.size() + 1/*In case the toSort is empty*/, new Comparator<String>() {
                @Override//from   w  w  w  .ja va 2 s.c o m
                public int compare(String lhs, String rhs) {
                    lhs = lhs.replaceAll("[^a-zA-Z0-9]", "");
                    rhs = rhs.replaceAll("[^a-zA-Z0-9]", "");
                    int result = rhs.compareTo(lhs);
                    return result;
                }
            });
    ordered.addAll(toSort);
    int originalSize = ordered.size();
    for (int i = 0; i < originalSize; i++) {
        sorted.add(ordered.poll());
    }
    return sorted;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes" })
private static Object toIntegerArray(Collection collection) {
    int[] ia = new int[collection.size()];

    int i = 0;//w w w  .  j ava  2 s  .  com
    for (Object o : collection) {
        int j = ((Integer) o).intValue();
        ia[i++] = j;
    }

    return ia;
}

From source file:Main.java

public static File[] listFiles(File directory, boolean recurse, FileFilter filter) {
    if (!recurse) {
        return directory.listFiles(filter);
    } else {//from  w ww .j ava2 s .c  om
        Collection<File> mFiles = new java.util.LinkedList<File>();
        innerListFiles(mFiles, directory, filter);
        return (File[]) mFiles.toArray(new File[mFiles.size()]);
    }
}

From source file:Main.java

public static <T> List<T> conjunction(Collection<T> c1, Collection<T> c2) {

    if (c1 == null || c2 == null) {

        return null;

    } else {/*  www  . ja  va  2 s .  c  o  m*/

        List<T> result = new ArrayList<T>(c1.size());

        for (T o : c1) {

            if (c2.contains(o)) {

                result.add(o);
            }
        }
        return result;
    }
}

From source file:org.ssoup.backend.web.RdfModelConverter.java

private static MediaType[] buildMediaTypeArray() {
    // now register the media types this converter can handle
    Collection<Lang> languages = RDFLanguages.getRegisteredLanguages();
    MediaType[] mediatypes = new MediaType[languages.size()];

    int i = 0;/*from www  .j a  v  a2 s  . c  o  m*/
    for (Lang lang : languages) {
        ContentType ct = lang.getContentType();
        logger.debug("registering converter for rdf content type {}", lang.getContentType());
        MediaType mt = new MediaType(ct.getType(), ct.getSubType());
        mediatypes[i++] = mt;
    }
    return mediatypes;
}