- 删除map空值
Map<String, String> params=new HashMap<>(); params.put("ccc","null"); params.put("bbb",""); params.put("aaa",null); params.put("ggg",null); Iterator<Map.Entry<String, String>> it = params.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, String> entry = it.next(); if(entry.getValue() == null) it.remove();//使用迭代器的remove()方法删除元素 }
Map<String, String> params=new HashMap<>(); params.put("ccc","null"); params.put("bbb",""); params.put("aaa",null); params.put("ggg",null); // 一行lamada表达式 params.entrySet().removeIf(entry -> entry.getValue() == null);