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:org.powertac.common.spring.SpringApplicationContext.java

/**
 * Returns the first Spring bean found that is an instance of the 
 * given class./*from   ww w. j av  a 2 s  .  c  o m*/
 */
public static <T> T getBeanByType(Class<T> type) {
    T bean = null;
    Collection<T> beans = context.getBeansOfType(type).values();
    if (beans.size() > 0) {
        bean = new ArrayList<T>(beans).get(0);
    }

    return bean;
}

From source file:com.shazam.fork.summary.OutcomeAggregator.java

public static Function<? super PoolSummary, Boolean> toPoolOutcome() {
    return new Function<PoolSummary, Boolean>() {
        @Override/*from w ww  .  j  av a  2 s  .  co m*/
        @Nullable
        public Boolean apply(@Nullable PoolSummary input) {
            final Collection<TestResult> testResults = input.getTestResults();
            final Collection<Boolean> testOutcomes = transform(testResults, toTestOutcome());
            return testOutcomes.size() > 0 && and(testOutcomes);
        }
    };
}

From source file:Main.java

public static String[] toArray(Collection<String> strings) {
    if (strings.isEmpty()) {
        return EMPTY_STRING_ARRAY;
    }//from w w w . j  a va  2 s  . c o m
    return strings.toArray(new String[strings.size()]);
}

From source file:Main.java

/**
 * Converts the given Collection into an array of the given type.
 *
 * @param collection The collection to convert
 * @param arrayType  The type of array to return
 * @return The new array/* www  .ja  v a2  s .c o m*/
 */
@SuppressWarnings({ "unchecked" })
public static <E> E[] toArray(Collection<E> collection, Class<? extends E> arrayType) {
    return collection.toArray((E[]) Array.newInstance(arrayType, collection.size()));
}

From source file:Main.java

public static <T> int nullSafeSize(final Collection<T> collection, final int nullValue) {
    if (collection == null) {
        return nullValue;
    }/*from   w  w w .j a  v  a2 s  . c  o m*/
    return collection.size();
}

From source file:HtmlUtil.java

/**
 * One of a pair of methods (the other is splitValues) that is used to combine several
 * un-encoded values into a single delimited, encoded value for placement into a
 * hidden field./* ww w . jav a 2  s.  c  om*/
 *
 * @param values One or more values which are to be combined
 * @return a single HTML-encoded String that contains all the values in such a way that
 *         they can be converted back into a Collection of Strings with splitValues().
 */
public static String combineValues(Collection<String> values) {
    if (values == null || values.size() == 0) {
        return "";
    } else {
        StringBuilder builder = new StringBuilder(values.size() * 30);
        for (String value : values) {
            builder.append(value).append(FIELD_DELIMITER_STRING);
        }

        return encode(builder.toString());
    }
}

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p>//from  www . jav  a  2 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 collections is non-empty
 * @since 2.1
 * @see #intersection
 */
public static boolean containsAny(final Collection coll1, final Collection 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 Long[] collectionToArray(Collection<String> collection) {
    if (collection == null || collection.isEmpty()) {
        return null;
    }//from   w w w . j  a  v a  2s .  c om
    Long[] arry = new Long[collection.size()];
    int index = 0;
    for (String value : collection) {
        arry[index++] = Long.valueOf(value);
    }
    return arry;
}

From source file:Main.java

/**
 * Gets the elements of received type within the received list.
 * /*  ww w .  ja  v  a 2s  . c  om*/
 * @param pList
 *            the list
 * @param pType
 *            the type to search for
 * 
 * @return the elements of the received type
 */
@SuppressWarnings("unchecked")
public static <C> List<C> getElementsOfType(final Collection<?> pList, final Class<C> pType) {
    final List<C> list = new ArrayList<C>();

    if (pList != null && pList.size() > 0) {
        synchronized (pList) {
            for (final Object obj : pList) {
                if (pType.isAssignableFrom(obj.getClass())) {
                    list.add((C) obj);
                }
            }
        }
    }

    return list;
}

From source file:Main.java

/**
 * //from  www  . j  ava2  s.  c o m
 * @param collection
 * @return int[]
 */
public static int[] toIntArray(final Collection<Integer> collection) {
    final int[] array = new int[collection == null ? 0 : collection.size()];

    if (collection != null) {
        int i = 0;

        for (Iterator<Integer> iterator = collection.iterator(); iterator.hasNext();) {
            array[i++] = iterator.next().intValue();
        }
    }

    return array;
}