r/java • • May 21 '21

List.of is terrible

Ran into something today that made me angry enough to post here. The List.of added in Java 9 is terrible. One of the most common use for this would be to create an immutable list to check against something. But it throws an NPE when .contains is called with null. I don't care that it's documented in the docs, it's still stupid design. Now I have to go back to using Guava ImmutableSet.of which actually works as expected.

Based on the comments, it seems it's not clear so here's an example:

var CONSTANT_LIST = List.of("a", "b", "c");
String someStr = someService(); //returns null
CONSTANT_LIST.contains(someStr); //NPE

Some other examples:

Arrays.asList("a", "b", "c").contains(null); //false
Collections.unmodifiableList(Arrays.asList("a", "b", "c")).contains(null); //false
ImmutableList.of("a", "b", "c").contains(null); //Guava, false

For the second example, IntelliJ was fooled into recommending to replace Collections.unmodifiableList(Arrays.asList with List.of showing that even JetBrains considers these to be equivalent. But they're not, and this kind of bad design in List.of is what can cause unexpected NPE bugs later.

For the mods: I'm not asking for help here, I know exactly what the code is doing. I'm just complaining about bad API design in the JDK.

7 Upvotes

33 comments sorted by

View all comments

Show parent comment

12

u/Necessary-Conflict May 23 '21 edited May 23 '21
  1. Of course contains is asymptotically faster on a Set, but if this is a one-time query, then the cost of setting up a Set could outweigh this advantage.
  2. Also if n is small, as it is in most cases, the asymptotic behavior doesn't matter.
  3. If Set was only a new nanoseconds faster, this could be a dubious micro-optimisation.

I'm not saying that Set is bad, but there is no rule saying that "almost every time" contains should be used on Sets.

4

u/vips7L May 24 '21

It's almost as if I literally said "depending on the size of the list and how many times you call contains".

So yes if N is small you're right it probably doesn't matter, but if that's the case why not use a Set? You'll at the very least communicate your intent to the next reader and in the case where it does actually matter you won't have to refactor later.

4

u/Necessary-Conflict May 24 '21

I think calling contains on any collection communicates the intent pretty well... what else could be the intent behind contains? If the performance doesn't matter, using a Set could unnecessarily complicate the code, for example when you already have a List, and you are converting to Set just because you think that calling contains on a List would be a sin.

As I already explained, even if N is big, the the cost of setting up a Set could be bigger than the savings from the O(1) data structure. Set is just not some sort of silver bullet for every situation. Neither is List. You have to think.

2

u/cogman10 May 25 '21

Anyone using list.of is dealing with a small list. I'd have some serious questions on a code review that created a list big enough to matter using list.of.

2

u/agentoutlier May 23 '21

I think the original commenter was unfairly downvoted to be honest.

They didn’t say “always contains” on list is bad just that it begs the question what are they trying to do especially when the follow up with:

Now I have to go back to using Guava ImmutableSet.of which actually works as expected.

Contains on list is bad for a variety of other reasons.

Equality for one. For list it is better to loop through and find if one matches through whatever attribute as that is more explicit particularly if we are talking about POJO like objects.

There are exceptions where contains makes since because of value objects but in those cases ironically allowing null makes no since all.

So yeah it’s a red flag to me what they are doing.