spring判空工具类_Java判断不为空的工具类总结

1 package com.bie.util;2

3 import java.util.Collection;4 import java.util.Iterator;5 import java.util.Map;6

7 /**8 *9 * @author biehl10 *11 * @date 2018年7月31日下午2:40:4012 *13 * @Notes 判断是否为空的工具栏,如果不使用StringUtils的jdk的工具类,可以自行封装14 *15 */

16 public classObjectUtils {17

18

19 /**20 * 判断字符串不为空21 * @param str22 * @return23 */

24 public staticboolean notEmpty(String str){25 //StringUtils.isNotEmpty(str);

26 return str != null && !"".equals(str);27 }28

29 /**30 * 判断字符串不为空31 * jdk StringUtils工具类实现如下所示32 * @param str33 * @return34 */

35 public staticboolean isNotEmpty(String str){36 return !isEmpty(str);37 }38

39 /**40 * 判断字符串为空41 * @param str42 * @return43 */

44 public staticboolean isEmpty(String str){45 return str == null || str.length() == 0;46 }47

48 /**49 * 集合判断是否为空50 * @param collection 使用泛型51 * @return52 */

53 public static boolean notEmpty(Collectioncollection){54 if(collection != null){55 Iterator iterator =collection.iterator();56 if(iterator != null){57 while(iterator.hasNext()){58 Object next =iterator.next();59 if(next != null){60 return true;61 }62 }63 }64 }65 return false;66 }67

68 /**69 * map集合不为空的判断70 * @param map 使用泛型,可以传递不同的类型参数71 * @return72 */

73 public static boolean notEmpty(Mapmap){74 return map != null && !map.isEmpty();75 }76

77 /**78 * byte类型数组判断不为空79 * @param t80 * @return81 */

82 public static boolean notEmpty(byte[] t){83 return t != null && t.length > 0;84 }85

86 /**87 * short类型数组不为空判断88 * @param t89 * @return90 */

91 public static boolean notEmpty(short[] t){92 return t != null && t.length > 0;93 }94

95 /**96 * 数组判断不为空,没有泛型数组,所以还是分开写吧97 * @param t 可以是int,short,byte,String,Object,long98 * @return99 */

100 public static boolean notEmpty(int[] t){101 return t != null && t.length > 0;102 }103

104 /**105 * long类型数组不为空106 * @param t107 * @return108 */

109 public static boolean notEmpty(long[] t){110 return t != null && t.length > 0;111 }112

113 /**114 * String类型的数组不为空115 * @param t116 * @return117 */

118 public staticboolean notEmpty(String[] t){119 return t != null && t.length > 0;120 }121

122 /**123 * Object类型数组不为空124 * @param t125 * @return126 */

127 public staticboolean notEmpty(Object[] t){128 return t != null && t.length > 0;129 }130

131 /**132 *133 * @param o134 * @return135 */

136 public staticboolean notEmpty(Object o){137 return o != null && !"".equals(o) && !"null".equals(o);138 }139

140 public static voidmain(String[] args) {141 //String str = "";142 //1、判断字符串是否为空notEmpty()方法

143 /*if(ObjectUtils.notEmpty(str)){144 System.out.println("字符串 " + str + " 不为空......");145 }else{146 System.out.println("字符串 " + str + "为空......");147 }*/

148

149 //2、判断字符串是否为空isNotEmpty()方法

150 /*if(ObjectUtils.isNotEmpty(str)){151 System.out.println("字符串 " + str + " 不为空......");152 }else{153 System.out.println("字符串 " + str + "为空......");154 }*/

155

156 //3、集合判断是否为空,list和set实现Collection

157 /*List list = new ArrayList();158 //list.add("hello");159 if(ObjectUtils.notEmpty(list)){160 System.out.println("List集合不为空");161 }else{162 System.out.println("List集合为空");163 }*/

164

165 /*Set set = new HashSet();166 set.add("hello");167 if(ObjectUtils.notEmpty(set)){168 System.out.println("set集合不为空");169 }else{170 System.out.println("set集合为空");171 }*/

172

173 //4、map集合接口,需要写单独的判读是否为空的方法

174 /*Map map = new HashMap();175 //map.put("hello", "hello world");176 if(ObjectUtils.notEmpty(map)){177 System.out.println("map集合不为空");178 }else{179 System.out.println("map集合为空");180 }*/

181

182 //5、数组判断不为空

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

189

190 /*byte[] b = new byte[]{1,2,3,4,5};191 if(ObjectUtils.notEmpty(b)){192 System.out.println("byte类型数组不为空");193 }else{194 System.out.println("byte类型数组为空");195 }196

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

204

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

212

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

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

226

227

228 }229

230 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值