Java Collection Empty isEmpty(Collection collection)

Here you can find the source of isEmpty(Collection collection)

Description

Helper to check if the collection is null or empty.
Collection#isEmpty() is not static and therefore require additional check for null .

License

MIT License

Parameter

Parameter Description
collection A collection to be checked

Return

true if is not null and contains at least one element. false otherwise.

Declaration

public static boolean isEmpty(Collection<?> collection) 

Method Source Code


//package com.java2s;
/*//from   ww w  .ja  va2s.co  m
 * Copyright 2014 Maxim Dominichenko
 * 
 * Licensed under The MIT License (MIT) (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * https://github.com/domax/gwt-dynamic-plugins/blob/master/LICENSE
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.Collection;
import java.util.Map;

public class Main {
    /**
     * Helper to check if the string is {@code null} or empty.<br/>
     * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}.
     * 
     * @param string
     *          A string to be checked
     * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise.
     */
    public static boolean isEmpty(String string) {
        return string == null || string.isEmpty();
    }

    /**
     * Helper to check if the array is {@code null} or empty.<br/>
     * 
     * @param array
     *          An array to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }

    /**
     * Helper to check if the collection is {@code null} or empty.<br/>
     * {@link Collection#isEmpty()} is not static and therefore require additional check for {@code null}.
     * 
     * @param collection
     *          A collection to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Helper to check if the map is {@code null} or empty.<br/>
     * {@link Map#isEmpty()} is not static and therefore require additional check for {@code null}.
     * 
     * @param map
     *          A map to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
}

Related

  1. isEmpty(Collection c)
  2. isEmpty(Collection c)
  3. isEmpty(Collection c)
  4. isEmpty(Collection col)
  5. isEmpty(Collection col)
  6. isEmpty(Collection collection)
  7. isEmpty(Collection collection)
  8. isEmpty(Collection collection)
  9. isEmpty(Collection collection)