移除键为空的键值对


有一个map里边的key是空,那么要移除掉这个空key的数据:


/*移除键为空的键值对*/
public static void removeNullKey(Map map) {
Set set = map.keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
Object obj = (Object) iterator.next();
remove(obj, iterator);
}
}

private static void remove(Object obj, Iterator iterator) {
if (obj instanceof String) {
String str = (String) obj;
if (str == null || str.trim().isEmpty()) {
iterator.remove();
}
} else if (obj instanceof Collection) {
Collection col = (Collection) obj;
if (col == null || col.isEmpty()) {
iterator.remove();
}

} else if (obj instanceof Map) {
Map temp = (Map) obj;
if (temp == null || temp.isEmpty()) {
iterator.remove();
}

} else if (obj instanceof Object[]) {
Object[] array = (Object[]) obj;
if (array == null || array.length <= 0) {
iterator.remove();
}
} else {
if (obj == null) {
iterator.remove();
}
}
}

输出的结果: