移除Map中空的键值对

 1 import java.util.*;
 2 
 3 public class RemoveNullKeyValueUtils {
 4     /*移除Map中值为空的键值对*/
 5     public static void removeNullEntry(Map map) {
 6         removeNullKey(map);
 7         removeNullValue(map);
 8     }
 9     /*移除键为空的键值对*/
10     public static void removeNullKey(Map map) {
11         Set set = map.keySet();
12         for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
13             Object obj = (Object) iterator.next();
14             remove(obj, iterator);
15         }
16     }
17     /*移除值为空的键值对*/
18     public static void removeNullValue(Map map) {
19         Set set = map.keySet();
20         for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
21             Object obj = (Object) iterator.next();
22             Object value = (Object) map.get(obj);
23             remove(value, iterator);
24         }
25     }
26 
27     private static void remove(Object obj, Iterator iterator) {
28         if (obj instanceof String) {
29             String str = (String) obj;
30             if (str == null || str.trim().isEmpty()) {
31                 iterator.remove();
32             }
33         } else if (obj instanceof Collection) {
34             Collection col = (Collection) obj;
35             if (col == null || col.isEmpty()) {
36                 iterator.remove();
37             }
38 
39         } else if (obj instanceof Map) {
40             Map temp = (Map) obj;
41             if (temp == null || temp.isEmpty()) {
42                 iterator.remove();
43             }
44 
45         } else if (obj instanceof Object[]) {
46             Object[] array = (Object[]) obj;
47             if (array == null || array.length <= 0) {
48                 iterator.remove();
49             }
50         } else {
51             if (obj == null) {
52                 iterator.remove();
53             }
54         }
55     }
56 }

 

转载于:https://www.cnblogs.com/llawliet0001/p/9835114.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值