The title says it all. It is a simple question really. Just wanted to understand how both these stuffs really work. Which would be the best option to return an immutable empty list? Would it be better to do Collections.emptyList() or ImmutableList.of() or is there a third and better option?
I would use Collections.emptyList()
because
1) why use a 3rd party library when you already have the same in JDK
2) Collections.emptyList()
returns a real simple private class Collections.EMPTY_LIST
(see Collections.java in JDK).
Whereas ImmutableList.of()
returns a Guava immutable list with all the functionality as if really containing objects, initializing it with new Object[0]
.
If you only need the empty list then you do not need the ImmutableList
functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With