Java Collection Utility methods - Trim/notNull for Collection

Similar to the trim utility method for Array. below is another simple but useful Java Collection Utility/Util method, which can be used to trim/remove all null Object values in a Collection object. Doing this would eliminate a null check on every element in collection. This method returns null value if the input Collection itself is null. We can call this as trim of Collection, as it will return all not null objects in the result(trimming the size of Collection).

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]



trim values in java, trim collection, trim list, trim java util ArrayList, trim strings in collection, trim collection size

Post a Comment Default Comments

  1. 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

    ReplyDelete
  2. A 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

    ReplyDelete
  3. Of course it can be done on the keySet() of a HashMap. But wouldn't be too bright, really.

    Because 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)

    ReplyDelete
  4. (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).

    Oh, and everyone should just stop reinventing the wheel and start using google collections.

    ReplyDelete
  5. I agree with @Dimitris Andreou on map trim, it would not be worth doing I guess.

    About 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.

    ReplyDelete

Individuals who comment on FromDev at regular basis, will be rewarded in Top Commenter section. (Comments are selectively moderated so please do not spam)

emo-but-icon

...

Follow Us

Get Updates By Email

item