Sormuras
Sormuras

Reputation: 9099

List.of() or Collections.emptyList()

As an special case of List.of(...) or Collections.unmodifiableList() - what is the preferred Java 9 way of pointing to an empty and immutable list?

Keep writing

Collections.emptyList();

or switch to

List.of();

Upvotes: 95

Views: 45462

Answers (5)

cquezel
cquezel

Reputation: 4497

Appart from the List.of() that behaves like a non null element List, the major difference is that Collections.emptyList() is a better replacement for a list that is used in further processing. For example, the following calls are perfectly legal on an empty List and will pass with Collections.emptyList() but will fail with List.of():

List<Integer> emptyList = Collections.emptyList();
emptyList.removeIf(i -> i > 10);
emptyList.sort(null);
emptyList.replaceAll(i -> i > 10 ? 10 : i);
emptyList.removeAll(List.of(1, 2, 3));
emptyList.retainAll(List.of(1, 2, 3));
emptyList.addAll(List.of()); // This is not a good idea!!

Upvotes: 0

AriyaDey
AriyaDey

Reputation: 325

As of JDK 11, If you look at the source code, you see that List.of(), uses a one-time initialized empty list, similar to Collections.emptyList(). So, I'll prefer to use List.of() because:

  • It is a more recent API. So, I think if Java maintainers were satisfied with the previous ways, they wouldn't add a new API.
  • It is more concise.
  • If you use List.of(E... elements) (And you should) for non-empty lists, you can use List.of() for empty ones and enjoy a unified look and feel and get rid of most of the factory methods in Collections API.

Upvotes: 16

Andrea Ciccotta
Andrea Ciccotta

Reputation: 672

  1. emptyList() creates a new empty list instance only once
  2. there is no difference on Readability: maybe List.of() is shorten than Collections.emptyList() but you can use a static import like import static java.util.Collections.emptyList; and then write only emptyList()

Upvotes: 4

user1803551
user1803551

Reputation: 13427

What is the preferred Java 9 way of pointing to an empty and immutable list?

The difference is rather subtle so "preferred" depends on what you want to achieve. Some behavioral differences:

  • List.of will throw an exception on contains(null) invocations.
  • You can deserialize emptyList() on JDK 8 and previous, but not List.of.

In terms or conveying that you want an empty list, emptyList() might look better, but this is just a temporary convention. If developers start using List.of() (which is much shorter than Collections.emptyList()) then it will become a known and accepted way, it's just new. If you think about it, there are some constructs we use which do not always convey what they do by themselves, but we got accustomed to them.

So there is no strictly preferred way. If the behavior does not matter use whatever you want.

Upvotes: 47

Collections.emptyList() does not need to create a new object for each call; it's typical, as in OpenJDK, to just return the singleton EMPTY_LIST object. Additionally, it's clearer that you intend to mean an empty list rather than having forgotten to fill in a placeholder.

Use emptyList(); it's both faster (up to Java target level 1.9) and more readable.

Upvotes: 60

Related Questions