Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections.emptyList() vs guava's ImmutableList.of()

Tags:

java

guava

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?

like image 750
Clint Avatar asked Apr 08 '17 07:04

Clint


1 Answers

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.

like image 73
Alexander Kulyakhtin Avatar answered Oct 25 '22 09:10

Alexander Kulyakhtin