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

/**
 * Checks to see if a collection is null or empty.
 * // www.  j a  v  a  2  s.  co m
 * @param collection
 *            Collection to test, may be null.
 * @return true if the collection is null or empty, otherwise false.
 */
public static boolean isEmpty(final Collection<?> collection) {
    return collection == null || collection.size() < 1;
}

From source file:Main.java

public static <K, V> Map<K, V> map(Collection<Entry<K, V>> entries) {
    Map<K, V> map = new LinkedHashMap<>(entries.size());
    for (Entry<K, V> entry : entries) {
        map.put(entry.getKey(), entry.getValue());
    }/*from w  w w. ja va2s. c  o m*/
    return map;
}

From source file:Main.java

public static <T> boolean isSubset(Collection<T> subset, Collection<T> superset) {
    if ((superset == null) || (superset.size() == 0)) {
        return ((subset == null) || (subset.size() == 0));
    }//from  w ww.  j a  va  2  s  .co  m

    HashSet<T> hash = new HashSet<T>(superset);
    for (T t : subset) {
        if (!hash.contains(t)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static String join(Collection<?> input, String sep) {
    if (input == null || input.size() == 0) {
        return "";
    }/*from   ww w  .j a  v  a2  s . co  m*/
    StringBuilder sb = new StringBuilder();
    int index = 0;
    int size = input.size();
    Iterator<?> it = input.iterator();
    while (it.hasNext()) {
        if (index == size - 1) {
            break;
        }
        Object o = it.next();
        sb.append(o).append(sep);
        index++;
    }
    sb.append(it.next());
    return sb.toString();
}

From source file:Main.java

public static <T extends Comparable<T>> T max(final Collection<T> collection) {
    if (collection == null || collection.size() <= 0) {
        return null;
    }/*  w  ww . ja  v a2s .c om*/
    T max = null;
    for (final T elem : collection) {
        if (max == null || elem.compareTo(max) > 0) {
            max = elem;
        }
    }
    return max;
}

From source file:Main.java

public static <T extends Comparable<T>> T min(final Collection<T> collection) {
    if (collection == null || collection.size() <= 0) {
        return null;
    }//from w  ww .  j  ava  2s.  com
    T max = null;
    for (final T elem : collection) {
        if (max == null || elem.compareTo(max) < 0) {
            max = elem;
        }
    }
    return max;
}

From source file:Main.java

public static Collection<?> filterByValue(String propertyName, Object value, Collection<?> collection) {
    List<Object> list = new ArrayList<Object>(collection.size());
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (propertyValue.equals(value)) {
            list.add(o);/*from w  w w  .  j a  v  a2s.  c o  m*/
        }
    }
    return list;
}

From source file:Main.java

public static Collection<?> filterByNotValue(String propertyName, Object value, Collection<?> collection) {
    List<Object> list = new ArrayList<Object>(collection.size());
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (!propertyValue.equals(value)) {
            list.add(o);/*  w ww. j a  v  a 2s . c  om*/
        }
    }
    return list;
}

From source file:Main.java

public static int countCollectionValues(Map<?, ? extends Collection<?>> map) {
    int counter = 0;
    for (Collection<?> c : map.values()) {
        if (c != null)
            counter += c.size();
    }//from   www  .  j a v  a2  s. co m
    return counter;
}

From source file:Main.java

public static <T> Collection<T> union(Collection<T> a, Collection<T> b) {
    HashSet<T> s = new HashSet<T>(a.size() + b.size());
    s.addAll(a);/*from   w ww.  j av a 2  s  .c  o m*/
    s.addAll(b);
    return s;
}