Java Array Empty Check isEmpty(Object[] array)

Here you can find the source of isEmpty(Object[] array)

Description

Helper to check if the array is null or empty.

License

Open Source License

Parameter

Parameter Description
array An array to be checked

Return

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

Declaration

public static boolean isEmpty(Object[] array) 

Method Source Code

//package com.java2s;
/*//from  w w  w .  j a v  a  2s.c o m
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2015 Maksym Dominichenko
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.*;

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.length() == 0;
    }

    /**
     * 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(Object[] array)
  2. isEmpty(Object[] array)
  3. isEmpty(Object[] array)
  4. isEmpty(Object[] array)
  5. isEmpty(Object[] array)
  6. isEmpty(Object[] array)
  7. isEmpty(Object[] array)
  8. isEmpty(Object[] array)
  9. isEmpty(Object[] array)