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> T getUnique(Collection<T> list) {
    if (list == null)
        throw new IllegalArgumentException(" collection is null! collection must be not null");
    else if (list.size() != 1)
        throw new IllegalArgumentException(
                list.size() + " collection must be unique : " + list.iterator().next().toString());
    else/*from  w  w w  . j ava  2 s .  com*/
        return list.iterator().next();
}

From source file:Main.java

/**
 * Determines if a collection is empty/*from   w  w  w  .ja  va  2  s .  com*/
 * 
 * @param collection 
 * @return true if the collection is empty
 */
@SuppressWarnings("unchecked")
public static boolean isEmpty(Collection collection) {
    boolean empty = false;
    if (collection == null) {
        empty = true;
    } else if (collection.size() <= 0) {
        empty = true;
    }
    return empty;
}

From source file:Main.java

/**
 * // w  w w  .ja  v a 2  s . co  m
 * @param c1
 * @param s1
 * @return c1 - s1
 * 
 */
public static <T> List<T> minius(Collection<T> c1, Collection<T> s1) {

    if (c1 == null) {
        return Collections.emptyList();
    }

    if (s1 == null || s1.size() == 0) {
        return new ArrayList<T>(c1);
    }
    List<T> list = new ArrayList<T>();
    for (T t : c1) {
        if (!s1.contains(t)) {
            list.add(t);
        }
    }
    return list;
}

From source file:Main.java

public static String[] asArray(Object object) {
    if (object == null) {
        return null;
    }/*from w  ww .  ja  v a  2s  .com*/

    Class componentType = object.getClass().getComponentType();

    if (String.class.equals(componentType)) {
        return (String[]) object;
    } else if (componentType != null) {
        Object[] objects = (Object[]) object;
        String[] result = new String[objects.length];
        for (int i = 0; i < objects.length; i++) {
            Object o = objects[i];
            if (o == null) {
                continue;
            }

            result[i] = o.toString();
        }

        return result;
    } else if (object instanceof Collection) {
        Collection collection = (Collection) object;
        String[] result = new String[collection.size()];
        Iterator iterator = collection.iterator();

        for (int i = 0; i < result.length; i++) {
            Object next = iterator.next();
            if (next == null) {
                continue;
            }

            result[i] = next.toString();
        }

        return result;
    } else {
        String string = object.toString().trim();
        String[] split = string.split("\\s*,\\s*");
        return split;
    }
}

From source file:Main.java

/**
 * Join a bunch of objects into a string delimited by the separator
 *
 * @param sep//from   w w  w.j  ava  2s . co  m
 * @param c
 * @return
 */
public static String join(final String sep, final Collection<? extends Object> c) {
    if (c == null) {
        return null;
    }
    if (c.size() == 1) {
        return String.valueOf(c.iterator().next());
    }
    if (c.size() == 0) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    String s = "";
    for (final Object o : c) {
        sb.append(s).append(String.valueOf(o));
        s = sep;
    }
    return sb.toString();
}

From source file:com.goncalomb.bukkit.bkglib.namemaps.EnchantmentsMap.java

public static List<String> getNames(Collection<Enchantment> enchantments) {
    ArrayList<String> names = new ArrayList<String>(enchantments.size());
    for (Enchantment enchantment : enchantments) {
        names.add(getName(enchantment));
    }/*from   w w  w.j  a  va2 s . co  m*/
    return names;
}

From source file:Main.java

public static boolean containsDuplicates(Collection<?> coll) {
    if (coll == null)
        throw new IllegalArgumentException();

    Set<Object> set = new HashSet<>(coll);

    return set.size() != coll.size();
}

From source file:Main.java

/**
 * Determines the size of the given collection, checking for null.
 *
 * @param   collection/*from w w w  .j a v  a  2  s.com*/
 *      The collection to get the size of.
 * @return
 *      The size of the collection. If it is null, zero is returned.
 */
public static int size(final Collection<?> collection) {
    if (collection == null) {
        return 0;
    } else {
        return collection.size();
    }
}

From source file:Main.java

public static String toString(Collection<Integer> stack, String delimiterChars) {
    if (stack.isEmpty()) {
        return "";
    }/*from   w w w .  j  av  a2 s .com*/
    if (stack.size() == 1) {
        return Integer.toString(stack.iterator().next());
    }
    StringWriter writer = new StringWriter();
    String delimiter = "";
    for (Integer item : stack) {
        writer.append(delimiter);
        writer.append(Integer.toString(item));
        delimiter = delimiterChars;
    }
    return writer.toString();
}

From source file:Main.java

public static long[] toPrimitiveLongArray(Collection<Long> collection) {
    // Need to do this manually because we're converting to a primitive long array, not
    // a Long array.
    final int size = collection.size();
    final long[] ret = new long[size];
    // Collection doesn't have get(i).  (Iterable doesn't have size())
    int i = 0;//from   www  .j  a va2  s. c o m
    for (Long value : collection) {
        ret[i++] = value;
    }
    return ret;
}