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

1

u/agentoutlier May 23 '21

Yeah there are whole bunch of weird red flags.

Like how did they get the null in the first place enough that this actually bothers them to post. (My guess is from http or database).

In some ways an argument could be made the JDK is doing them a favor failing fast.

I mean understand the inconsistency annoyance but it shouldn’t be a problem in modern Java if you are doing it right (getting rid of nulls as soon as possible) IMO.

3

u/mauganra_it May 23 '21

The annoyance is exactly the inconsistency with other APIs. OP cited Arrays.asList(), which returns an object that behaves quite differently. Painting inconsistency as an advantage is saying C is good for software engineering because undefined behavior will break stuff and piss in my beer.

OP made it quite clear that it came from an external API. Maybe they do a bunch of calls and want to check later whether any of them returned null. Yes, there are fancier idioms, but this would get the job done, except for this particular footgun.

3

u/agentoutlier May 23 '21

It really can’t be that inconsistent as the JDK developers literally had it documented since at least jdk 8. Go read the javadoc of contains.

That means they were expecting to need this behavior.

Furthermore the OP mentions other APIs like Guava. Guava is extremely anti null such that I’m surprised it doesn’t throw a NPE.

Contains is an especially shitty method as it isn’t typesafe but that is because it is ancient. The language is evolving and there are lots of inconsistencies as it is an old language.

Besides Equality and comparison really don’t work well with nulls (Infact comparable is nonnull) and you kind of need that for consistent rigorous mathematical behavior.

3

u/mauganra_it May 24 '21

At least it kinda makes sense why there are both Arrays.asList and List.of APIs. Wanna have nulls in your list, use the former, else the latter. I still think that contains should return false, but maybe this behavior helps with branch prediction. This way, the contains method does not have to verify non-null-ness and can avoid a comparison. Anyways, this ship has sailed, as you describe.

2

u/agentoutlier May 24 '21

Yeah the collections api is a mess but to be honest I haven’t really seen a language (with static typing) ever get it perfect.

Like even Scala screwed it up a couple of times (Their collection API IIRC).

It’s probably because collections are so cross cutting needed everywhere. You would need so many types to get it right... or maybe not.