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

public static <T extends Comparable<T>> int compareAsSet(@Nonnull Collection<? extends T> set1,
        @Nonnull Collection<? extends T> set2) {
    int res = Ints.compare(set1.size(), set2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> sortedSet1 = toNaturalSortedSet(set1);
    SortedSet<? extends T> sortedSet2 = toNaturalSortedSet(set2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }/*w ww . j a  va  2  s .  c o m*/
    return 0;
}

From source file:com.ibm.bluej.commonutil.DenseVectors.java

public static double[] toPrimativeArray(Collection<Double> l) {
    double[] d = new double[l.size()];
    int ndx = 0;/*from w  w w .  j  a v a  2  s . com*/
    for (Double di : l) {
        d[ndx++] = di;
    }
    return d;
}

From source file:com.framework.infrastructure.utils.Collections3.java

/**
 * (Getter), List./*from   w  ww .jav  a2 s . c  om*/
 * 
 * @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 ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:Main.java

public static String deepToString(final Collection<?> collection, final String left, final String right,
        final String sep) {
    final StringBuilder sb = new StringBuilder(3 * collection.size());
    sb.append(left);/*  w w  w  .ja v a  2s . c om*/
    final Iterator<?> iter = collection.iterator();
    if (iter.hasNext())
        sb.append(iter.next());
    while (iter.hasNext()) {
        sb.append(sep);
        sb.append(iter.next());
    }
    sb.append(right);
    return sb.toString();
}

From source file:Main.java

/**
 * Returns a list of strings from al collection of arbitrary objects.
 *
 * Uses the <code>toString()</code> operation of every collection element.
 * Null elements set to null in the list.
 *
 * @param  coll collection//from  www  . j a v  a  2 s.  com
 * @return      list of strings
 */
public static List<String> toStringList(Collection<?> coll) {
    if (coll == null) {
        throw new NullPointerException("coll == null");
    }

    List<String> list = new ArrayList<>(coll.size());

    for (Object o : coll) {
        if (o == null) {
            list.add(null);
        } else {
            list.add(o.toString());
        }
    }

    return list;
}

From source file:Main.java

public static int size(Collection<? extends Object> collection) {
    if (collection == null) {
        return 0;
    }//from  w w  w . j  a va2s  .  c  om
    return collection.size();
}

From source file:Main.java

public static <T> T getElement(Collection<T> c, int index, T defElement) {
    if (isEmpty(c) || index >= c.size()) {
        return defElement;
    }/*from   www  .  ja  va 2 s  .  c  o m*/

    if (index < 0) {
        index = c.size() + index;
        if (index < 0) {
            return defElement;
        }
    }

    if (c instanceof List) {
        return ((List<T>) c).get(index);

    } else {
        for (T element : c) {
            if (index-- == 0) {
                return element;
            }
        }

        // unreachable
        return defElement;
    }
}

From source file:Main.java

/**
 * Returns an arbitrary element from the collection
 *//*from  w w w .j  a  v  a2 s.  c  o m*/
public static <T> T getAnElement(Collection<T> coll) {
    if (coll == null) {
        throw new IllegalArgumentException("Called with null collection");
    } else if (coll.size() == 0) {
        return null;
    }
    if (coll instanceof List) {
        return ((List<T>) coll).get(0);
    }
    Iterator<T> it = coll.iterator();
    return it.next();
}

From source file:com.cloudera.oryx.ml.param.RandomSearch.java

/**
 * @param ranges ranges of hyperparameters to try, one per hyperparameters
 * @param howMany how many combinations of hyperparameters to return
 * @return combinations of concrete hyperparameter values
 *//*from  ww  w .  j a  v a2 s  . c o  m*/
static List<List<?>> chooseHyperParameterCombos(Collection<? extends HyperParamValues<?>> ranges, int howMany) {
    Preconditions.checkArgument(howMany > 0);

    int numParams = ranges.size();
    if (numParams == 0) {
        return Collections.singletonList(Collections.emptyList());
    }

    RandomDataGenerator rdg = new RandomDataGenerator(RandomManager.getRandom());
    List<List<?>> allCombinations = new ArrayList<>(howMany);
    for (int i = 0; i < howMany; i++) {
        List<Object> combination = new ArrayList<>(numParams);
        for (HyperParamValues<?> range : ranges) {
            combination.add(range.getRandomValue(rdg));
        }
        allCombinations.add(combination);
    }

    return allCombinations;
}

From source file:Main.java

/**
 * Samples without replacement from a collection, using your own
 * {@link Random} number generator.//  ww w  . j a  va 2  s  .  co m
 *
 * @param c
 *          The collection to be sampled from
 * @param n
 *          The number of samples to take
 * @param r
 *          the random number generator
 * @return a new collection with the sample
 */
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {
    if (n < 0)
        throw new IllegalArgumentException("n < 0: " + n);
    if (n > c.size())
        throw new IllegalArgumentException("n > size of collection: " + n + ", " + c.size());
    List<E> copy = new ArrayList<E>(c.size());
    copy.addAll(c);
    Collection<E> result = new ArrayList<E>(n);
    for (int k = 0; k < n; k++) {
        double d = r.nextDouble();
        int x = (int) (d * copy.size());
        result.add(copy.remove(x));
    }
    return result;
}