Java中常用的判空操作

package org.tools;

import java.util.List;
import java.util.Map;

/**
 * Java中常用的判空操作
 * 
 * @author qiao.wang
 * @version 1.0
 *
 */
public class Empty4J {
	
	/**
	 * 判断字符串为空
	 * 
	 * @param str 字符串
	 * @return 若字符串不存在 或者 字符串去空格后为空(length为0),返回true
	 */
	public static boolean stringIsEmpty(String str) {
		/**
		 * isEmpty()从JDK1.6开始
		 */
		return str == null || str.trim().isEmpty();
	}
	
	/**
	 * 判断字符串不为空
	 * 
	 * @param str 字符串
	 * @return 若字符串存在 并且 字符串去空格后不为空(length>0),返回true
	 */
	public static boolean stringIsNotEmpty(String str) {
		return !stringIsEmpty(str);
	}
	
	/**
	 * 判断list列表为空
	 * @param <T> 泛型
	 * 
	 * @param list List列表
	 * @return 若list列表不存在 或者 list列表不包含元素,返回true
	 */
	public static <T> boolean listIsEmpty(List<T> list) {
		return list == null || list.isEmpty();
	}
	
	/**
	 * 判断list列表不为空
	 * @param <T> 泛型
	 * 
	 * @param list List列表
	 * @return 若list列表存在 并且 list列表包含元素,返回true
	 */
	public static <T> boolean listIsNotEmpty(List<T> list) {
		return !listIsEmpty(list);
	}
	
	/**
	 * 判断map映射为空
	 * @param <K> 泛型
	 * @param <V> 泛型
	 * 
	 * @param map Map映射
	 * @return 若map映射不存在 或者map映射未包含键-值映射关系,返回true
	 */
	public static <K, V> boolean mapIsEmpty(Map<K, V> map) {
		return map == null || map.isEmpty();
	}
	
	/**
	 * 判断map映射不为空
	 * @param <K> 泛型
	 * @param <V> 泛型
	 * 
	 * @param map Map映射
	 * @return 若map映射存在 并且map映射包含键-值映射关系,返回true
	 */
	public static <K, V> boolean mapIsNotEmpty(Map<K, V> map) {
		return !mapIsEmpty(map);
	}

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值