returns true if the specified collection is null or empty - Java java.util

Java examples for java.util:Collection Search

Description

returns true if the specified collection is null or empty

Demo Code


//package com.java2s;

import java.util.Collection;

import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out.println(isEmpty(collection));
    }//  w ww .  j  av  a  2 s .  co  m

    /**
     * This is a convenience method that returns true if the specified collection is null or empty
     * 
     * @param <T>
     *            Any type of object
     * @param collection
     * @return
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * This is a convenience method that returns true if the specified map is null or empty
     * 
     * @param <T>
     *            any type of key
     * @param <U>
     *            any type of value
     * @param map
     * @return
     */
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }
}

Related Tutorials