Example usage for java.util Collection isEmpty

List of usage examples for java.util Collection isEmpty

Introduction

In this page you can find the example usage for java.util Collection isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:Main.java

/**
 * Create a textual representation of the given collection, separating the individual elements by the provided separator.
 * //  ww w  .j  av a  2s.c o  m
 * @param collection
 *            the collection to represent as text
 * @param separator
 *            the separator to include between elements of the collection
 * @return the colleciton's textual representation (or an empty String if the collection is {@code null} or empty)
 */
public static String toString(final Collection<?> collection, final String separator) {
    if (collection == null || collection.isEmpty()) {
        // just return an empty String if the collection is null or empty
        return "";
    }
    if (separator == null) {
        // fall back to the collection's toString() method if no custom separator has been specified
        return collection.toString();
    }
    // guess at a meaningful initial size
    final StringBuilder builder = new StringBuilder(collection.size() * (16 + separator.length()));
    boolean addSeparator = false;
    // iterate over the individual elements
    for (final Object collectionElement : collection) {
        if (addSeparator) {
            builder.append(separator);
        } else {
            addSeparator = true;
        }
        // null elements are treated as empty Strings
        if (collectionElement != null) {
            builder.append(collectionElement.toString());
        }
    }
    return builder.toString();
}

From source file:Main.java

public static boolean isEmpty(Collection collection) {
    System.out.println("3333333333333333333333");
    return (collection == null) || collection.isEmpty();
}

From source file:Main.java

public static Map groupMap(Collection collection, String keyName) {
    Map map = new HashMap();
    if (collection == null || collection.isEmpty()) {
        return map;
    }//from   w  ww .j  ava  2  s .c  o  m

    Map eachMap = null;
    Object key = null;
    List groupList = null;

    Iterator iter = collection.iterator();

    while (iter.hasNext()) {
        eachMap = (Map) iter.next();
        key = eachMap.get(keyName);
        if (key == null) {
            continue;
        }
        if (map.containsKey(key)) {
            groupList = (List) map.get(key);
            groupList.add(eachMap);
        } else {
            groupList = new ArrayList();
            groupList.add(eachMap);
            map.put(key, groupList);
        }
    }
    return map;
}

From source file:Main.java

public static <E extends Comparable<E>> E getSmallest(final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }//from   ww w .  ja  v a2  s.  c  o  m

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallest((List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element.compareTo(result) < 0) {
            result = element;
        }
    }

    return result;
}

From source file:Main.java

public static <E> E getSmallest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/* w ww .jav a2  s .c  o m*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallest(comparator, (List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (comparator.compare(element, result) < 0) {
            result = element;
        }
    }

    return result;
}

From source file:Main.java

public static <E extends Comparable<E>> E getGreatest(final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/*ww  w.  j  a  v a  2  s .  c om*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatest((List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element.compareTo(result) > 0) {
            result = element;
        }
    }

    return result;
}

From source file:Main.java

public static <E> E getGreatest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/*from   www  .  j  a v a 2s  . c  o  m*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatest(comparator, (List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (comparator.compare(element, result) > 0) {
            result = element;
        }
    }

    return result;
}

From source file:Main.java

public static boolean isEmpty(Collection<?> items) {
    if (items == null) {
        return true;
    }//  w w  w  . ja v  a 2s. c  o m
    return items.isEmpty();
}

From source file:com.romeikat.datamessie.core.base.ui.component.DocumentsFilter.java

private static String generateDocumentsString(final Collection<Long> documentIds) {
    if (documentIds == null || documentIds.isEmpty()) {
        return null;
    }/*w  ww  .  j  a va 2s. com*/
    final String documentsString = StringUtils.join(documentIds, " ");
    return documentsString;
}

From source file:Main.java

/**
 * Returns a list of the elements invoking toString on non-null elements.
 * @param collection to render// w  ww .  j a v  a 2s.  c om
 * @param <T> type
 * @return comma-separate list of values (no escape)
 */
public static <T> String toString(Collection<T> collection) {
    if (collection == null) {
        return "null";
    }
    if (collection.isEmpty()) {
        return "";
    }
    StringBuilder buf = new StringBuilder();
    String delimiter = "";
    for (T t : collection) {
        if (t == null) {
            continue;
        }
        buf.append(delimiter);
        buf.append(t);
        delimiter = ", ";
    }
    return buf.toString();
}