Wednesday, November 7, 2007

Java Collections API silly gotcha

I am embarrassed to admit this but here goes… This is not the first time I have had to relearn that the Java collections sort methods do not handle null entries. If you have an Object[] or ArrayList that has null entries in it, Arrays.sort() and Collections.sort() fail with NullPointerExceptions. I should have this burned into my memory but… I don’t. Anyway the only way to get around this is to sort it yourself eternally (that seems dorky to me), cull out the null entries or use the Collections.sort(List, Comparator) version. Implementing the comparator is (IMHO) the way to go but, it is kind of verbose. This time I had a few extra minutes to think of a different (and hopefully better) way. One of my favorite external APIs is Jakarta Collections, and they have a nice short way of implementing the sort…


I think that this is sweet. I am hoping to get a chance to explore replacing Jakarta Collections with Google collections as it seems to be a little bit more active and takes advantage of generics.

No comments:

Post a Comment