java判断数组为空的工具有哪些呢?java判断不为空的工具类又有哪些呢?

2020-03-25 11:53:44 java常见问答 11290

在Java开发过程中,我们经常会对其代码进行判断,比如字符串,数组,集合等,简单的列子介绍比如用户登录,我们首先要判断用户输入是否为空,然后使用ajax来做校验的功能,所以小编今天为大家讲解,java判断数组为空用到了什么工具类?以及Java判断不为空的工具又有哪些?

JAVA ArrayUtils 数组工具类:

package com.sicdt.library.core.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
 *
 * <br>类 名: 集合,数组工具 
 * <br>描 述: 描述类完成的主要功能
 */
public class ArrayUtils {

    /**
     * 判断是否为空
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection<?> collection){
        return collection == null || collection.isEmpty();
    }

    /**
     * 判断是否为空
     * @param map
     * @return
     */
    public static boolean isEmpty(Map<?, ?> map){
        return map == null || map.isEmpty();
    }

    /**
     * 判断是否为空
     * @param array
     * @return
     */
    public static boolean isEmpty(Object[] array){
        return array == null || array.length == 0;
    }

    /**
     * 判断是否为空
     * @param array
     * @return
     */
    public static boolean isEmpty(List<Object> array){
        return array == null || array.size() == 0;
    }

    public static boolean isArray(Object object) {
        if(object == null){
            return false;
        }
        return object.getClass().isArray();
    }

    public static boolean isCollection(Object object) {
        return object instanceof Collection;
    }

    @SuppressWarnings("unchecked")
    public static <T> T[] objectToArray(Object obj) {
        if(obj == null){
            return (T[])new Object[0];
        }
        if(isArray(obj)){
            return (T[])obj;
        }else if(isCollection(obj)){
            return (T[]) ((Collection<T>)obj).toArray();
        }
        return (T[]) new Object[]{obj};
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> objectToList(Object obj){
        if(obj == null){
            return Collections.emptyList();
        }
        if(isArray(obj)){
            return Arrays.asList((T[])obj);
        }else if(isCollection(obj)){
            return new ArrayList<T>((Collection<T>)obj);
        }
        List<T> list = new ArrayList<T>();
        list.add((T) obj);
        return list;
    }

    public static int size(Object obj){
        if(obj == null){
            return 0;
        }
        if(isArray(obj)){
            return ((Object[])obj).length;
        }
        if(isCollection(obj)){
            return ((Collection<?>)obj).size();
        }
        return -1;
    }

    public static <T> boolean contains(T[] array, Object obj){
        return Arrays.asList(array).contains(obj);
    }
}

Java判断是否为空的工具类,可以直接使用。包含,String字符串,数组,集合等等。具体代码以及注释如下:

public class ObjectUtils {
    /**
     * 判断字符串不为空
     *
     * @param str
     * @return
     */
    public static boolean notEmpty(String str) {
//StringUtils.isNotEmpty(str);
        return str != null && !"".equals(str);
    }

    /**
     * 判断字符串不为空
     * jdk StringUtils工具类实现如下所示
     *
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 判断字符串为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 集合判断是否为空
     *
     * @param collection 使用泛型
     * @return
     */
    public static <T> boolean notEmpty(Collection<T> collection) {
        if (collection != null) {
            Iterator<T> iterator = collection.iterator();
            if (iterator != null) {
                while (iterator.hasNext()) {
                    Object next = iterator.next();
                    if (next != null) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * map集合不为空的判断
     *
     * @param map 使用泛型,可以传递不同的类型参数
     * @return
     */
    public static <T> boolean notEmpty(Map<T, T> map) {
        return map != null && !map.isEmpty();
    }

    /**
     * byte类型数组判断不为空
     *
     * @param t
     * @return
     */
    public static boolean notEmpty(byte[] t) {
        return t != null && t.length > 0;
    }

    /**
     * short类型数组不为空判断
     *
     * @param t
     * @return
     */
    public static boolean notEmpty(short[] t) {
        return t != null && t.length > 0;
    }

    /**
     * 数组判断不为空,没有泛型数组,所以还是分开写吧
     *
     * @param t 可以是int,short,byte,String,Object,long
     * @return
     */
    public static boolean notEmpty(int[] t) {
        return t != null && t.length > 0;
    }

    /**
     * long类型数组不为空
     *
     * @param t
     * @return
     */
    public static boolean notEmpty(long[] t) {
        return t != null && t.length > 0;
    }

    /**
     * String类型的数组不为空
     *
     * @param t
     * @return
     */
    public static boolean notEmpty(String[] t) {
        return t != null && t.length > 0;
    }

    /**
     * Object类型数组不为空
     *
     * @param t
     * @return
     */
    public static boolean notEmpty(Object[] t) {
        return t != null && t.length > 0;
    }

    /**
     * @param o
     * @return
     */
    public static boolean notEmpty(Object o) {
        return o != null && !"".equals(o) && !"null".equals(o);
    }

    public static void main(String[] args) {
        //String str = "";
        //1、判断字符串是否为空notEmpty()方法
             /*if(ObjectUtils.notEmpty(str)){
          System.out.println("字符串 " + str + " 不为空......");
       }else{
           System.out.println("字符串 " + str + "为空......");
       }*/

        //2、判断字符串是否为空isNotEmpty()方法
            /*if(ObjectUtils.isNotEmpty(str)){
            System.out.println("字符串 " + str + " 不为空......");
      }else{
           System.out.println("字符串 " + str + "为空......");
        }*/

        //3、集合判断是否为空,list和set实现Collection
              /*List<String> list = new ArrayList<String>();
        //list.add("hello");
      if(ObjectUtils.notEmpty(list)){
            System.out.println("List集合不为空");
       }else{
            System.out.println("List集合为空");
        }*/

             /*Set<String> set = new HashSet<String>();
        set.add("hello");
     if(ObjectUtils.notEmpty(set)){
         System.out.println("set集合不为空");
        }else{
          System.out.println("set集合为空");
      }*/
        //4、map集合接口,需要写单独的判读是否为空的方法
                     /*Map<String, String> map = new HashMap<String, String>();
       //map.put("hello", "hello world");
         if(ObjectUtils.notEmpty(map)){
             System.out.println("map集合不为空");
        }else{
            System.out.println("map集合为空");
        }*/

        //5、数组判断不为空
                 /*int[] a = new int[]{1,2,3,4,5};
        if(ObjectUtils.notEmpty(a)){
            System.out.println("int类型数组不为空");
         }else{
            System.out.println("int类型数组为空");
       }*/
              /*byte[] b = new byte[]{1,2,3,4,5};
       if(ObjectUtils.notEmpty(b)){
          System.out.println("byte类型数组不为空");
        }else{
             System.out.println("byte类型数组为空");
       }

        short[] c = new short[]{1,2,3,4,5};
       if(ObjectUtils.notEmpty(c)){
             System.out.println("short类型数组不为空");
         }else{
           System.out.println("short类型数组为空");
        }


         long[] d = new long[]{1,2,3,4,5};
         if(ObjectUtils.notEmpty(d)){
             System.out.println("long类型数组不为空");
        }else{
             System.out.println("long类型数组为空");
        }


         String[] e = new String[]{"hello","world","lisi","zhangsan"};
         if(ObjectUtils.notEmpty(e)){
           System.out.println("String类型数组不为空");
        }else{
            System.out.println("String类型数组为空");
       }

       Object[] a = new Object[]{1,2,3,4,5};
        if(ObjectUtils.notEmpty(a)){
            System.out.println("Object类型数组不为空");
        }else{
             System.out.println("Object类型数组为空");
       }*/

    }
}

以上就是java判断数组为空的工具以及总结了Java中判断不为空的工具类,希望能给小伙伴们带来不一样的收获,想要了解跟多,请关注本网站。