If I have a rarely used collection in some class which may be instantiated many times, I may sometimes resort to the following "idiom" in order to save unnecessary object creations:
List<Object> list = null;
void add(Object object) {
if (list == null)
list = new ArrayList<Object>();
list.add(object);
}
// somewhere else
if (list != null)
for (Object object : list)
;
Now I was wondering if I couldn't eliminate those null checks using Collections.emptyList()
, however then I would have to alter the if check in add()
like so:
if (list == Collections.<Object>emptyList())
list = new ArrayList<Object>();
Is there a better way to handle this other than just allocating a new empty collection every time?
EDIT: just to be clear, I would like to use Collections.emptyList(), but the above check in add() is really really ugly... I was wondering if there's a better way to do it or even a whole other way of handling this.
If you only use the list for iterations, you could just use:
for (Object object : list)
which wouldn't do anything for empty lists, i.e. not a single iteration.Otherwise just check
list.isEmpty()
.The suggested answers are absolutly correct, just small tip - in Java 8 you can use the new Optional class to handle the case where list instance is null, in a more functional approach.
For example, something like this:
Here is what I use for a helper method in some of my code. Really works nicely in reducing the ton of null checks I'd normally have to place before iterating over lists. If you want a list that wouldn't be immutable then you can return a new list object instead of Collections.emptyList
You then just use the helper method like so:
If the list object is null, then the helper method will return the static empty list and the contents of your loop will be skipped. This is a nice way to avoid having to create null checks wrapping any list iterations.
I've made the internals of the list be of type Object just for the example, but you'd obviously change this to be whatever makes the most sense for your usage.
You can create a utility class with static methods, like:
}
That's a really bad idea which will litter your code with
== null
checks and other handling of corner cases (and presumably end up in null pointer exceptions anyway)!No, not really.
emptyList()
returns an empty list. You could dobut that will still throw a NullPointerException if
list == null
, so it's still not what you're after.My recommendation: Always initialize the list to
new ArrayList<Object>
, or, if you for instance want to return an empty list from a method, useCollections.emptyList()
instead. (This returns the same instance every time, so no unnecessary object creation there either.)And then use
.isEmpty()
to check if a collection is empty or not.I find it easiest to follow this convention:
If the point of my methods is to return a Collection, the method never returns null. Null is ambiguous. Instead I return
Collection.emptyXXX()
orImmutableXXX.of()
if using Guava.If I have an object which is maintaining an internal list as a member, instantiate it in the constructor. I try not to do lazy instantiation unless I can prove its a significant gain because lazy code, in my opinion, tends to be more difficult to debug when issues arise.
I really see immutable or unmodifiable empty collections being a part of a contract external to objects. If you are using the collection internally, I can only really see using immutable collections if you have a good reason (concurrency, consistency, immutability of the object)