1、Java判斷是否為空的工具類,可以直接使用。包含,String字符串,數組,集合等等。
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 biehl 10 * 11 * @date 2018年7月31日下午2:40:40 12 * 13 * @Notes 判斷是否為空的工具欄,如果不使用StringUtils的jdk的工具類,可以自行封裝 14 * 15 */ 16 public class ObjectUtils { 17 18 19 /** 20 * 判斷字符串不為空 21 * @param str 22 * @return 23 */ 24 public static boolean notEmpty(String str){ 25 //StringUtils.isNotEmpty(str); 26 return str != null && !"".equals(str); 27 } 28 29 /** 30 * 判斷字符串不為空 31 * jdk StringUtils工具類實現如下所示 32 * @param str 33 * @return 34 */ 35 public static boolean isNotEmpty(String str){ 36 return !isEmpty(str); 37 } 38 39 /** 40 * 判斷字符串為空 41 * @param str 42 * @return 43 */ 44 public static boolean isEmpty(String str){ 45 return str == null || str.length() == 0; 46 } 47 48 /** 49 * 集合判斷是否為空 50 * @param collection 使用泛型 51 * @return 52 */ 53 public static <T> boolean notEmpty(Collection<T> collection){ 54 if(collection != null){ 55 Iterator<T> 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 * @return 72 */ 73 public static <T> boolean notEmpty(Map<T, T> map){ 74 return map != null && !map.isEmpty(); 75 } 76 77 /** 78 * byte類型數組判斷不為空 79 * @param t 80 * @return 81 */ 82 public static boolean notEmpty(byte[] t){ 83 return t != null && t.length > 0; 84 } 85 86 /** 87 * short類型數組不為空判斷 88 * @param t 89 * @return 90 */ 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,long 98 * @return 99 */ 100 public static boolean notEmpty(int[] t){ 101 return t != null && t.length > 0; 102 } 103 104 /** 105 * long類型數組不為空 106 * @param t 107 * @return 108 */ 109 public static boolean notEmpty(long[] t){ 110 return t != null && t.length > 0; 111 } 112 113 /** 114 * String類型的數組不為空 115 * @param t 116 * @return 117 */ 118 public static boolean notEmpty(String[] t){ 119 return t != null && t.length > 0; 120 } 121 122 /** 123 * Object類型數組不為空 124 * @param t 125 * @return 126 */ 127 public static boolean notEmpty(Object[] t){ 128 return t != null && t.length > 0; 129 } 130 131 /** 132 * 133 * @param o 134 * @return 135 */ 136 public static boolean notEmpty(Object o){ 137 return o != null && !"".equals(o) && !"null".equals(o); 138 } 139 140 public static void main(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<String> list = new ArrayList<String>(); 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<String> set = new HashSet<String>(); 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<String, String> map = new HashMap<String, String>(); 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 }
待續......