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.enonic.cms.core.content.contenttype.ContentTypeKey.java

public static int[] convertToIntArray(Collection<ContentTypeKey> keys) {
    int[] array = new int[keys.size()];
    int index = 0;
    for (ContentTypeKey key : keys) {
        array[index++] = key.toInt();//from  w  w w .j av a 2s .  c  om
    }
    return array;
}

From source file:Main.java

/**
 * Returns the size of a collection, or zero if the collection is null.
 * //from w w w  .ja v  a 2s . c  o  m
 * @param collection
 *            The collection to inspect, may be null.
 * @return The size of the collection, zero if the collection is null.
 */
public static int safeSize(final Collection<?> collection) {
    if (collection == null) {
        return 0;
    }
    return collection.size();
}

From source file:com.talis.entity.TestUtils.java

public static void assertQuadCollectionsEqual(Collection<Quad> first, Collection<Quad> second) {
    assertEquals(first.size(), second.size());
    assertTrue(first.containsAll(second));
}

From source file:Main.java

/**
 * Converts a collection to a primitive array of a given type.
 *
 * @param type       type of array to create
 * @param collection collection to convert, must contain items that extend T
 * @param <T>        type of array to create
 * @return primitive array of type T/*  w  w  w.ja va2  s  .  c  o m*/
 */
public static <T> T[] toArray(Class<T> type, Collection<? extends T> collection) {
    T[] array = (T[]) Array.newInstance(type, collection.size());
    int i = 0;
    for (T item : collection) {
        array[i++] = item;
    }

    return array;
}

From source file:de.zib.gndms.stuff.misc.LanguageAlgebra.java

/**
 * Get the largest prefix shared by a set of words.
 *
 * @param words The set of words to get the largest prefix from.
 * @return The greatest common prefix.//from w w  w  . jav a  2 s.com
 */
public static String getGreatestCommonPrefix(Collection<String> words) {
    if (null == words || 0 == words.size()) {
        throw new IllegalArgumentException(
                "Cannot build the greatest common prefix out of an empty set of words!");
    }

    String commonprefix = words.iterator().next();

    for (String w : words) {
        commonprefix = getCommonPrefix(commonprefix, w);
    }

    return commonprefix;
}

From source file:Main.java

/**
 * Determine the size of a specified collection.
 * /*from  ww  w.ja va2  s .  c om*/
 * @param collection - collection to determine the size of
 * @return size of the collection
 */
@SuppressWarnings("unchecked")
public static int size(Collection collection) {
    if (collection == null) {
        return 0;
    } else {
        return collection.size();
    }
}

From source file:Main.java

public static <T> int compareAsList(@Nonnull Comparator<? super T> elementComparator,
        @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }//from   www.j  a v a2s  .c  om
    return 0;
}

From source file:Main.java

/**
 * Creates a map with the elements of the collection as values using the
 * specified keyMethod to obtain the key from the elements.
 *//*from   w ww  .  ja v  a2  s  .  c  o m*/
@SuppressWarnings("unchecked")
public static <K, T> Map<K, T> createMap(Collection<T> collection, String keyMethod) {
    Map<K, T> map = new HashMap<>(collection.size());

    if (collection.isEmpty()) {
        return map;
    }

    Class<?> elementClass = collection.iterator().next().getClass();

    Method getKeyMethod;

    try {
        getKeyMethod = elementClass.getMethod(keyMethod, new Class[0]);
    } catch (Exception e) {
        throw new RuntimeException("Failed to get key method", e);
    }

    for (T element : collection) {
        K key;
        try {
            key = (K) getKeyMethod.invoke(element, (Object[]) null);
        } catch (Exception e) {
            throw new RuntimeException("Failed to get key", e);
        }

        map.put(key, element);
    }

    return map;
}

From source file:com.enonic.cms.core.log.LogEntryKey.java

public static List<LogEntryKey> convertToList(Collection<String> collection) {
    if (collection == null || collection.size() == 0) {
        return null;
    }// www.  j  a v  a  2s  .c o m

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

    }
    return list;
}

From source file:com.qwazr.utils.ArrayUtils.java

public static long[] toPrimitiveLong(Collection<Long> collection) {
    long[] array = new long[collection.size()];
    int i = 0;//from ww  w  . j  a  v a 2 s . com
    for (long val : collection)
        array[i++] = val;
    return array;
}