1. 程式人生 > >java檢測集合是否為空或長度是否為0

java檢測集合是否為空或長度是否為0

java檢測集合是否為空或長度是否為0

當讀資料庫的資料後,返回的資料可能為空或長度等於0,而我們要取第一個,或其他的資料,如果這個資料為空時就會報錯,寫了一個工具類方便呼叫:

package com.yx.utils;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ObjectIfNullUtils {

    /**
     * 判斷物件是否為空
     *
     * @param obj
     * @return
     */
    public static Boolean ifNull(Object obj) {
        return obj == null ? true : false;
    }

    @Test
    public void test(){

    }

    /**
     * 判斷物件長度是否為空(物件不能為null)
     *
     * @param type
     * @param obj
     * @return
     */
    public static Boolean ifLengthZero(String type, Object obj) {
        int length = 0;
        switch (type) {
            case "Collection":
                if(!(obj instanceof  Collection)){
                    throw new RuntimeException("這個不是一個Collection集合");
                }
                length = ((Collection) obj).size();
                break;
            case "Map":
                if(!(obj instanceof  Map)){
                    throw new RuntimeException("這個不是一個Map集合");
                }
                length = ((Map) obj).size();
                break;
            case "String":
                if(!(obj instanceof  String)){
                    throw new RuntimeException("這個不是一個String物件");
                }
                length = ((String) obj).length();
                break;
        }
        return length == 0 ? true : false;
    }

    /**
     * 判斷物件是否為空或者長度等於0
     *
     * @param type
     * @param obj
     * @return
     */
    private static Boolean ifObjectLengthZeroAndNull(String type, Object obj) {
        if (ifNull(obj)) {
            return true;
        }
        if (ifLengthZero(type, obj)) {
            return true;
        }
        return false;
    }

    /**
     * 判斷List集合是否為空或長度是否等於0
     *
     * @param list
     * @return
     */
    public static Boolean ifListLengthZeroAndNull(List list) {
        return ifObjectLengthZeroAndNull("Collection", list);
    }

    /**
     * 判斷Set集合是否為空或長度是否等於0
     *
     * @param set
     * @return
     */
    public static Boolean ifSetLengthZeroAndNull(Set set) {
        return ifObjectLengthZeroAndNull("Collection", set);
    }

    /**
     * 判斷Map集合是否為空或長度是否等於0
     *
     * @param map
     * @return
     */
    public static Boolean ifMapLengthZeroAndNull(Map map) {
        return ifObjectLengthZeroAndNull("Map", map);
    }

    /**
     * 判斷String集合是否為空或長度是否等於0
     *
     * @param str
     * @return
     */
    public static Boolean ifStringLengthZeroAndNull(String str) {
        return ifObjectLengthZeroAndNull("String", str);
    }

    /**
     * 判斷Array集合是否為空或長度是否等於0
     *
     * @param obj
     * @return
     */
    public static Boolean ifArrayLengthZeroAndNull(Object obj) {
        if (ifNull(obj)) {
            return true;
        }
        if (obj.getClass().isArray()) {
            int length = Array.getLength(obj);
            if (length == 0) {
                return true;
            }else {
                return false;
            }
        }else{
            throw new RuntimeException("這個不是一個數組");
        }
    }

    /**
     * 返回一個指定的集合數(超出範圍會拋異常)
     * @param list
     * @param num
     * @param <T>
     * @return
     */
    public static <T> T getListNumDate(List<T> list, Integer num){
        if(!ifListLengthZeroAndNull(list)) {
            return list.get(num);
        }
        return null;
    }
}