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.
Errick robertson
source share