Using NULL as a key value on a map

I would like to know if using NULL as a key in a Map object is considered a good style, and if not, what are the alternatives?

+7
source share
6 answers

This is usually not considered a good style.

Since NULL usually indicates an undefined or undefined value, it is usually confused to use it as a key to the map. (although there may be specific circumstances where this makes sense)

Alternatives depend on the situation, so let me use an example. Let's say that we want the color-specific lines in the text, and we also need the default color for the text that does not match any of the lines.

 private HashMap<String,Color> stringColors; private Color defaultColor; 

Instead of saving the default color in the HashMap with the NULL key, put the default color in a specific variable. This makes it really clear to anyone looking at the code what exactly this color means.

I would say that the driving factor is that you actually have a NULL value somewhere that you can directly look for on the map. (this does not happen often, in my experience). In this specific example that I gave, the default color will be used for any lines that are not on the map. In this case, there is no NULL string that requires color for.

+8
source

I personally would recommend using a Null Object (see Null Object Pattern ) or some default key, rather than null as a key. Using null always means that you need to protect your code from a NullPointerException , and should be avoided whenever possible.

Edit

As an example, if you create an API that allows users to add values โ€‹โ€‹to a map (whether it be a home implementation of Map or a utility class that uses a map to store its data) and your first version, let them use null as a key, you will always have to support null keys. Thus, either your Map support should be an implementation that accepts null keys, or you will have to catch the sucker keys and replace them with another key that will represent the null key.

As @Erick Robertson mentioned, there are times when accepting null keys makes sense. However, if you are developing an API, you must really make sure that you want to handle such keys, as this always means that more code is needed to check for null . And that also means more security code for your API clients.

+3
source

Consider using something like DefaultedMap , which returns a known value if the card does not contain the given key.

Depending on your use case, it might be cleaner than using (a) a null , or (b) NullObject. If you describe your actual use, it will be easier to help.

0
source

In general, null keys and null values โ€‹โ€‹are bad.

There are two great pages on the Google Guava site that explain how and why not to use Null's.

LivingWithNullHostileCollections - how to handle collections that don't allow null

UsingAndAvoidingNullExplained - Guava tools to use and avoid using null, explained.

Quote:

But what if?

What if you find that you want to place a null element in one of these null-hostile animals?

  • If in a set or as a key on a card - no; this is clearer (less surprising) if you explicitly specify null in the search process
  • If the value on the map is to leave this entry; save a separate set of non-zero keys (or null keys)
  • If on the list - if the list is sparse, can you use a map?
  • Consider whether there is a natural โ€œnull objectโ€ that can be used. Not always. But sometimes. For example: if this is an enumeration, add a constant to indicate what you expect from null.
  • Just use a different collection implementation, e.g. Collections.unmodifiableList (Lists.newArrayList ()) instead of ImmutableList.
  • Mask of zeros (more information required)
  • Use Optional
0
source

why do you use null as a key for a map object? using an ojbect map becomes no more useful if most of the map object's methods depend on a key parameter.

-one
source

It is sometimes useful to keep the default value on the map as a Null value. I do not know other cases. But this is legal, so why not do it if it can bring some profit?

-one
source

All Articles