A notNull method for simple object is already a common utility method, which is used by many programmers now. Checking Collection with the same notNull can also be a good idea. I am not sure how much performance boost we can get by doing this but I am open to thoughts on it. Please share your comment with us.
Below is the implementation of notNull method for collection and its unit test methods for java.util.List and java.util.Set implementations.
/**
* This Collection utility or util method can be used to trim all the null
* values in the collection. For a collection with data {"1", "2", null,
* "3","4"} the output will be {"1", "2", "3","4"}
*
* @param values
* @return Collection
*/
@SuppressWarnings("unchecked")
public static Collection notNull(final Collection values) {
if (values == null) {
return null;
}
Iterator colItr = values.iterator();
while (colItr.hasNext()) {
Object obj = colItr.next();
if (obj == null) {
colItr.remove();
}
}
return values;
}
Above method should consistently behave for all type of collections. Below are the testing results for above method when tested for a Set.
private static void testSetNotNull() {
Collection hs = new HashSet();
hs.add("1");
hs.add("2");
hs.add("3");
hs.add(null);
hs.add("4");
System.out.println(notNull(hs));
}
Output for above test method is as shown below, (remember that Set doesn't guarantee sequence of objects)
[3, 2, 1, 4]
Same test is run for a List and the results are as expected.
private static void testListNotNull() {
List l = new ArrayList();
l.add("1");
l.add("2");
l.add("3");
l.add(null);
l.add("4");
System.out.println(notNull(l));
}
Output for above test method is as shown below
[1, 2, 3, 4]
Why can't we do the same for HashMap too? Map doesn't inherit Collection, but it can return a set of keys which can be traversed
ReplyDeleteA map would have a key-value pair and you can have a null value in HashMap not in Hashtable ...so does it really worth the effort for having a notNull method only for hashMap? Ma $0.02
ReplyDeleteOf course it can be done on the keySet() of a HashMap. But wouldn't be too bright, really.
ReplyDeleteBecause it would only be a single key, and instead of doing this:
JavaCollectionUtilities.trim(map.keySet());
you could as easily do:
map.removeKey(null); //done
(AJ, not only a HashMap allows null keys: how about TreeSet/TreeMap for example)
(In case I confuse someone, I don't suggest map.removeKey(null) because it happened to be less characters, but because it's hugely haster).
ReplyDeleteOh, and everyone should just stop reinventing the wheel and start using google collections.
I agree with @Dimitris Andreou on map trim, it would not be worth doing I guess.
ReplyDeleteAbout using Google collections, I guess its still not final ... they mention on their release page
"We are now posting release candidates for the final 1.0 release. Until the release is final, the API is still subject to change. (However, these changes should be very minimal at this point.) "
Google Collectionsnot sure if we should wait for it to become stable then start using it.