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 String join(Collection<?> collection, String seperator) {
    Object[] objs = new Object[collection.size()];
    collection.toArray(objs);/*from w  w w.  j a va 2s . co  m*/
    return join(objs, seperator);
}

From source file:Main.java

@SuppressWarnings("unchecked")
static <E> Collection<E> reverse(Collection<? extends E> c) {
    Object[] reverse = new Object[c.size()];
    int index = c.size();
    for (E e : c) {
        reverse[--index] = e;//from  w w  w. j  a v  a2s.c om
    }

    return (List<E>) Arrays.asList(reverse);
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection substract(Collection source, Collection target) {
    if (source == null || source.size() == 0) {
        return null;
    }/* w  w  w .  jav  a2s .  co m*/
    if (target == null || target.size() == 0) {
        return source;
    }
    Collection result = new ArrayList();
    for (Iterator it = source.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (!target.contains(candidate)) {
            result.add(candidate);
        }
    }
    return result;
}

From source file:Main.java

public static Object findSingleObject(Collection c) {
    if (c == null || c.isEmpty())
        return null;
    if (c.size() > 1)
        throw new IllegalStateException("found more than one object when single object requested");
    return c.iterator().next();
}

From source file:Main.java

public static <E> E extractSingleton(Collection<E> collection) {
    if (collection != null && collection.size() == 1) {
        return collection.iterator().next();
    } else {//  www. j  a  v a2  s  . c om
        throw new IllegalArgumentException("Can extract singleton only when collection size == 1");
    }
}

From source file:Main.java

/**
 * Return true if same objects exist in listA and listB
 *///from   w  w  w.  ja  v  a  2s.co  m
public static <T> boolean isEqual(Collection<T> listA, Collection<T> listB) {
    if (listA.size() != listB.size()) {
        return false;
    }
    if (listA.size() != setIntersection(listA, listB).size()) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Returns the size of the collection or array
 *//*from  w  w w .j a va2 s.  co m*/
public static int size(Object collection) {
    if (collection instanceof Collection) {
        Collection value = (Collection) collection;
        return value.size();
    } else if (collection != null) {
        if (collection.getClass().isArray()) {
            return Array.getLength(collection);
        }
    }
    return 0;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection in(Collection source, Collection target) {
    if (source == null || source.size() == 0) {
        return null;
    }//from w  ww  . j  ava 2 s  .com
    if (target == null || target.size() == 0) {
        return null;
    }
    Collection result = new ArrayList();
    for (Iterator it = source.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (target.contains(candidate)) {
            result.add(candidate);
        }
    }
    return result;
}

From source file:Main.java

public static <T> boolean hasItems(Collection<T> source) {
    if (source == null)
        return false;

    return source.size() > 0;
}

From source file:Main.java

/**
 * @return true if collection has only 1 item (null safe).
 */// w w  w . j  a v  a 2  s .c om
public static boolean hasOneItem(final Collection<?> col) {
    return col != null && col.size() == 1;
}