java验证工具类_【Java工具】之验证工具类(七)

本篇文章主要介绍验证工具类,主要包括数组、字符串、集合、map以及对象是否为空的验证。

1.判断数组是否为空

/**

* 判断数组是否为空

* @param array

* @return boolean

*/

@SuppressWarnings("unused")

private static boolean isEmptyArray(T[] array){

if (array == null || array.length == 0){

return true;

}

else{

return false;

}

}

2.判断数组是否不为空

/**

* 判断数组是否不为空

* @param array

* @return boolean

*/

public static boolean isNotEmptyArray(T[] array){

if (array != null && array.length > 0){

return true;

}

else{

return false;

}

}

3.判断字符串是否为空

/**

* 判断字符串是否为空

* @param string

* @return boolean

*/

public static boolean isEmptyString(String string){

if (string == null || string.length() == 0){

return true;

}

else{

return false;

}

}

4.判断字符串是否不为空

/**

* 判断字符串是否不为空

* @param string

* @return boolean

*/

public static boolean isNotEmptyString(String string){

if (string != null && string.length() > 0){

return true;

}

else{

return false;

}

}

5.判断集合是否为空

/**

* 判断集合是否为空

* @param collection

* @return boolean

*/

public static boolean isEmptyCollection(Collection> collection){

if (collection == null || collection.isEmpty()){

return true;

}

else{

return false;

}

}

6.判断集合是否不为空

/**

* 判断集合是否不为空

* @param collection

* @return boolean

*/

public static boolean isNotEmptyCollection(Collection> collection){

if (collection != null && !collection.isEmpty()){

return true;

}

else{

return false;

}

}

7.判断map集合是否不为空

/**

* 判断map集合是否不为空

* @param map

* @return boolean

*/

@SuppressWarnings("rawtypes")

public static boolean isNotEmptyMap(Map map){

if (map != null && !map.isEmpty()){

return true;

}

else{

return false;

}

}

8.判断map集合是否为空

/**

* 判断map集合是否为空

* @param map

* @return boolean

*/

@SuppressWarnings("rawtypes")

public static boolean isEmptyMap(Map map){

if (map == null || map.isEmpty()){

return true;

}

else{

return false;

}

}

9.检验对象是否为空,String 中只有空格在对象中也算空

/**

* 检验对象是否为空,String 中只有空格在对象中也算空.

* @param object

* @return 为空返回true,否则false.

*/

@SuppressWarnings("rawtypes")

public static boolean isEmpty(Object object) {

if (null == object)

return true;

else if (object instanceof String)

return "".equals(object.toString().trim());

else if (object instanceof Iterable)

return !((Iterable) object).iterator().hasNext();

else if (object.getClass().isArray())

return Array.getLength(object) == 0;

else if (object instanceof Map)

return ((Map) object).size() == 0;

else if (Number.class.isAssignableFrom(object.getClass()))

return false;

else if (Date.class.isAssignableFrom(object.getClass()))

return false;

else

return false;

}

10.校验对象非空

/**

* 校验对象非空

* @param obj

* @return

*/

public static boolean isNotEmpty(Object obj)

{

return !isEmpty(obj);

}

d593188463f9

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值