This article describes the Java implementation of filtering out values with empty key or value in map collection. Share it for your reference, as follows:
import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;/** * Filter out the values of the key or value in the map collection with empty keys* @author lmb * @date 2017-3-14 */public class MapRemoveNullUtil { /** * Remove the empty key or value value in the map * @param map */ public static void removeNullEntry(Map map){ removeNullKey(map); removeNullValue(map); } /** * Remove the empty key of the map * @param map * @return */ 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); } } /** * Remove the empty value in the map * @param map * @return */ 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); } } /** * Remove null values in map* * Iterator works in an independent thread and has a mutex lock. * After the Iterator is created, a single-linked index table pointing to the original object will be created. When the number of original objects changes, the content of this index table will not change synchronously. * Therefore, when the index pointer moves backward, the object to be iterated cannot be found. Therefore, according to the fail-fast principle, the Iterator will immediately throw a java.util.ConcurrentModificationException exception. * Therefore, iterator does not allow iterated objects to be changed when it works. * But you can use the Iterator's own method remove() to delete the object. The Iterator.remove() method will maintain the consistency of the index while deleting the current iterated object. * @param obj * @param iterator */ private static void remove(Object obj,Iterator iterator){ if(obj instanceof String){ String str = (String)obj; if(isEmpty(str)){ //Filter out the output result of the main function with null and "" map: {2=BB, 1=AA, 5=CC, 8= }// if("".equals(str.trim())){ //Filter out the output result of the main function with null, "" and " " map: {2=BB, 1=AA, 5=CC} 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(); } } } public static boolean isEmpty(Object obj){ return obj == null || obj.toString().length() == 0; } public static void main(String[] args) { Map map = new HashMap(); map.put(1, "AA"); map.put("2", "BB"); map.put("5", "CC"); map.put("6",null); map.put("7", ""); map.put("8", " "); System.out.println(map);//Output result: {2=BB, 1=AA, 7=, 6=null, 5=CC, 8= } removeNullEntry(map); System.out.println(map); }}Running results:
{1=AA, 2=BB, 5=CC, 6=null, 7=, 8= }
{1=AA, 2=BB, 5=CC, 8= }
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.