ltd.icecold.orange.utils.RemoveNullKeyValueUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of OrangeAPI Show documentation
Show all versions of OrangeAPI Show documentation
The OrangeAPI provides some web API framework to easy develop
The newest version!
package ltd.icecold.orange.utils;
import java.util.*;
public class RemoveNullKeyValueUtils {
public static void removeNullEntry(Map map) {
removeNullKey(map);
removeNullValue(map);
}
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);
}
}
public static void removeNullValue(Map map) {
Set set = map.keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
Object obj = (Object) iterator.next();
Object value = (Object) map.get(obj);
remove(value, 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();
}
}
}
}