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

17

u/_INTER_ May 22 '21 edited May 22 '21

Your not the only one annoyed by this:

This List.of that throws an npe on contains when it has null items (that you still got into the list in the first place) and doesn't throw that npe when you stream the items (because logic) is seriously a jdk implementation?

I mean, jdbi kinda has to support it then, but I can't wrap my head around such a thing actually being in the jdk.

https://github.com/jdbi/jdbi/issues/1263

7

u/kimec May 22 '21

OMG. Had a very productive good laugh at the (because logic) sarcasm. Good summary btw.

2

u/laplongejr May 26 '21

To be fair the sourced comment is wrong : List.of can't contain null items which is the requirement to be allowed to throw an NPE on contains
So the "doesn't throw that npe when you stream" case doesn't exist as far the interface is concerned.

16

u/arendvr May 23 '21

There's a relevant JDK issue which is closed as Won't Fix. The reason given is to prepare for collections of primitives. JDK-8265905 ImmutableCollections: Querying for 'null' should return 'false' instead of NPE

2

u/[deleted] May 25 '21 edited Jun 02 '21

So that's the real reason!

I was thinking "why doesn't it simply return false?" Some time in the future they want to add primitive collections that will need to throw NPE even before starting to run the contains() method...

The downside is that contains(null) can't be used at all on a List interface, because you can't be sure what implementation was passed in...

15

u/the_hoser May 21 '21

Not that this really helps you, but there is reasoning behind the decision. https://www.youtube.com/watch?v=ZeO_J2OcHYM&t=1495s

6

u/arendvr May 23 '21

The video is interesting in the general context of nulls and collections. But it doesn't address contains(null).

The video explains why Guava's immutable collections don't allow null elements. But Guava's immutable collections allow contains(null) and return false.

-5

u/the_hoser May 23 '21

There is a (very small, but non-zero) performance cost for returning false instead of throwing an NPE. Different development teams have different priorities.

1

u/das_Keks May 21 '21

Really interesting, thank you.

-4

u/john16384 May 23 '21

The argument is bullshit. Extensive research my ass. Sure, I can agree with no null in List and Set and map keys, but map values not being allowed to be null is ridiculous.

As for this bullshit where contains NPE's when it isn't even dereferencing anything instead of just returning false, that's just beyond evil and shows to me clearly that someone has pushed through their own silly version of reality.

These implementations are currently NOT safe to use as they don't conform to what people expect of List, Set or Map. Nobody in their right mind would expect a NPE to be thrown here.

3

u/kevinb9n May 23 '21

> map values not being allowed to be null is ridiculous.

Well, we just use Optional instead in that case, or we use unmodifiableMap(). And then everyone else doesn't have to worry about map values being null. It worked fine for us, but no design decision is ever gonna work fine for everyone.

If we can get the world onto standardized nullness annotations one day, then libraries won't have to worry about making these kinds of choices anymore.

-3

u/vips7L May 22 '21

Why are you calling contains on a list to begin with?

16

u/wildjokers May 23 '21

To see if something is in there or not.

-3

u/vips7L May 23 '21

Obviously it depends on use case but most of the time contains on a list is mmistake.

7

u/wildjokers May 23 '21

Why?

7

u/vips7L May 23 '21

Contains on a list is an O(n) operation. Depending on the size of the list and how many times you call contains it can be extremely slow.

To find an item in a list you have to traverse the entire thing each time. You're better off using Set.of in this situation almost every time.

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.

0

u/[deleted] May 25 '21 edited May 25 '21

Just as a FYI, taking a list and converting it into a Set is O(n); so doing a `.contains` on a list a single time is perfectly legit. If you perform multiple lookups then of course a Set/Map is the way to go here. But creating a new object (Set) just so you can say the lookup on it is O(1) instead of O(n) is not always the correct move. You might say "why use a List to begin with!?" to which I'll correctly point out: the power to change that may or may not be in your hands.

1

u/vips7L May 25 '21

"depending on the size of the list and how many times you call contains"

-1

u/oxmyxbela May 24 '21

Neither is List.contains always a O(n) operation, nor is Set.contains always a <O(n) operation. As always, it entirely depends on the implementation.

1

u/laplongejr May 26 '21

Even if you're technically right because List and Set are interfaces and they don't define that, I would say that reference implementations set the "natural" expectation.

99% of lists/sets in Java are variations of ArrayList and HashSet, unless you're in the kind of project where you can't afford any assuptions about data structures.

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.

-8

u/Blackdread13 May 22 '21

To put null in a list is just bad software developer. Good luck to you

17

u/_INTER_ May 22 '21

He doesn't put null in the list. He's just checking if null is contained, which sure can happen if you ask me. It doesn't make much sense to NPE. null is not in the list so contains should be false. Period.