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> boolean hasDuplicates(Collection<T> collection) {
    Set<T> set = new HashSet<T>(collection);
    return set.size() != collection.size();
}

From source file:Main.java

public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
    if (c == null || c.size() == 0)
        return prefix + suffix;
    Iterator<T> it = c.iterator();
    String result = prefix + it.next();
    while (it.hasNext())
        result += sep + it.next();/*from w  w w  .ja v a  2s.  co m*/
    return result + suffix;
}

From source file:Main.java

/**
 * Assesses all the possible containment relations between collections A and B with one call.<br>
 * Returns an int with bits set, according to a "Venn Diagram" view of A vs B.<br>
 * NOT_A_SUPERSET_B: a - b != {}<br>
 * NOT_A_DISJOINT_B: a * b != {}  // * is intersects<br>
 * NOT_A_SUBSET_B: b - a != {}<br>
 * Thus the bits can be used to get the following relations:<br>
 * for A_SUPERSET_B, use (x & CollectionUtilities.NOT_A_SUPERSET_B) == 0<br>
 * for A_SUBSET_B, use (x & CollectionUtilities.NOT_A_SUBSET_B) == 0<br>
 * for A_EQUALS_B, use (x & CollectionUtilities.NOT_A_EQUALS_B) == 0<br>
 * for A_DISJOINT_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) == 0<br>
 * for A_OVERLAPS_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) != 0<br>
 *///from ww w  . j a v a 2  s.  c  o m
public static int getContainmentRelation(Collection a, Collection b) {
    if (a.size() == 0) {
        return (b.size() == 0) ? ALL_EMPTY : NOT_A_SUPERSET_B;
    } else if (b.size() == 0) {
        return NOT_A_SUBSET_B;
    }
    int result = 0;
    // WARNING: one might think that the following can be short-circuited, by looking at
    // the sizes of a and b. However, this would fail in general, where a different comparator is being
    // used in the two collections. Unfortunately, there is no failsafe way to test for that.
    for (Iterator it = a.iterator(); result != 6 && it.hasNext();) {
        result |= (b.contains(it.next())) ? NOT_A_DISJOINT_B : NOT_A_SUBSET_B;
    }
    for (Iterator it = b.iterator(); (result & 3) != 3 && it.hasNext();) {
        result |= (a.contains(it.next())) ? NOT_A_DISJOINT_B : NOT_A_SUPERSET_B;
    }
    return result;
}

From source file:Main.java

/**
 * Converts Collection to array. /*from   w ww  .  jav  a 2  s  .c o  m*/
 * @param <T>
 * @param col
 * @param cls
 * @return 
 * @see java.util.Collection#toArray(T[]) 
 */
public static <T> T[] toArray(Collection<T> col, Class<T> cls) {
    T[] arr = (T[]) Array.newInstance(cls, col.size());
    return col.toArray(arr);
}

From source file:Main.java

public static <T> List<T> concat(Collection<? extends T> xs, Collection<? extends T> ys) {
    ArrayList<T> result = new ArrayList<>(xs.size() + ys.size());
    result.addAll(xs);/*from  www. j  a v  a2 s .  c om*/
    result.addAll(ys);
    return result;
}

From source file:Main.java

public static <T> boolean isDistinct(Collection<T> in) {
    if (in == null) {
        return true;
    }//from ww  w  .  j  ava2 s  .  com
    return in.size() == distinct(in).size();
}

From source file:Main.java

public static boolean isEmpty(Collection<?> values) {
    return values == null || values.size() == 0;
}

From source file:com.appdynamicspilot.util.ArgumentUtils.java

public static boolean isNullOrEmpty(Collection argumentValue) {
    return (isNull(argumentValue) || argumentValue.size() == 0);
}

From source file:Main.java

public static int size(final Collection collection) {
    return null != collection ? collection.size() : 0;
}

From source file:Main.java

/**
 * Convert a Collection of Numbers to an array of primitive ints
 * Null values will be skipped in the array 
 * @param items//from w ww.j a va  2  s. com
 * @return
 */
public static int[] toIntArray(Collection<? extends Number> items) {
    int ret[] = new int[items.size()];
    int idx = 0;
    for (Number n : items) {
        if (n != null)
            ret[idx] = n.intValue();
        idx += 1;
    } // FOR
    return (ret);
}