Java集合中移除所有的null值
https://blog.csdn.net/qq_36135928/article/details/86605104
org.apache.commons.collections.subtract方法只能移除第一个null元素。
public class CollectionRemoveNullTest { @Test public void test() { List<String> nullList = new ArrayList<>(); nullList.add("a"); nullList.add(null); nullList.add("b"); nullList.add("c"); nullList.add(null); Collection<String> removeNull = subtract(nullList, Arrays.asList((String) null)); Iterator<String> it = removeNull.iterator(); while (it.hasNext()) { String s = it.next(); assertTrue(s != null); } } /** Returns a new {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>. The cardinality of each element <i>e</i> in the returned {@link Collection} will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality of <i>e</i> in <i>b</i>, or zero, whichever is greater. @param a the collection to subtract from, must not be null @param b the collection to subtract, must not be null @return a new collection with the results @see Collection#removeAll */ public static Collection subtract(final Collection a, final Collection b) { ArrayList list = new ArrayList(a); for (Iterator it = b.iterator(); it.hasNext()