Collections.emptyList() instead of null check?

后端 未结 9 1087
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 02:59

If I have a rarely used collection in some class which may be instantiated many times, I may sometimes resort to the following \"idiom\" in order to save unnecessary object crea

9条回答
  •  别那么骄傲
    2021-02-04 03:14

    You can create a utility class with static methods, like:

    public class ListUtil {
    
    /**
     * Checks if {@link List} is null or empty.
     *
     * @param  the generic type
     * @param list the list
     * @return true, if is null or empty
     */
    public static  boolean isNullOrEmpty(List list) {
        return list == null || list.size() == 0;
    }
    
    /**
     * Checks if {@link List} is not null and empty.
     *
     * @param  the generic type
     * @param list the list
     * @return true, if is not null and empty
     */
    public static  boolean isNotNullAndEmpty(List list) {
        return list != null && list.size() != 0;
    }
    

    }

提交回复
热议问题