What is the use of adding a null key or value in a HashMap in Java?

HashMap allows you to use one null key and any number of null values. What is the use of this?

+72
java hashmap null key
May 31 '10 at 18:26
source share
7 answers

I'm not sure what you are asking, but if you are looking for an example of when you want to use the null key, I often use them on maps to represent the default case (that is, the value that should be used if this key is missing):

Map<A, B> foo; A search; B val = foo.containsKey(search) ? foo.get(search) : foo.get(null); 

HashMap processes null keys specially (since it cannot call .hashCode() for a null object), but null values ​​are not something special, they are stored on the map, like everything else

+105
May 31 '10 at 18:32
source share

For example, to model trees. If you use a HashMap to represent a tree structure where the key is the parent and the value is a list of children, then the root nodes will be the values ​​for the null key.

+24
Mar 15 '13 at 20:04
source share

One use case for null values ​​is to use a HashMap as a cache for the results of an expensive operation (like calling an external web service) that can return null .

Entering a null value on the map allows you to distinguish between the case when the operation was not performed for the given key ( cache.containsKey(someKey) returns false ) and where the operation was performed but returned a null value ( cache.containsKey(someKey) returns true , cache.get(someKey) returns null ).

Without null values, you will need to either add some special value to the cache to indicate a null response, or simply not cache this answer at all and perform the operation each time.

+6
Jun 16 '15 at 9:35
source share

Here is my only somewhat contrived example of a case where the null key may be useful:

 public class Timer { private static final Logger LOG = Logger.getLogger(Timer.class); private static final Map<String, Long> START_TIMES = new HashMap<String, Long>(); public static synchronized void start() { long now = System.currentTimeMillis(); if (START_TIMES.containsKey(null)) { LOG.warn("Anonymous timer was started twice without being stopped; previous timer has run for " + (now - START_TIMES.get(null).longValue()) +"ms"); } START_TIMES.put(null, now); } public static synchronized long stop() { if (! START_TIMES.containsKey(null)) { return 0; } return printTimer("Anonymous", START_TIMES.remove(null), System.currentTimeMillis()); } public static synchronized void start(String name) { long now = System.currentTimeMillis(); if (START_TIMES.containsKey(name)) { LOG.warn(name + " timer was started twice without being stopped; previous timer has run for " + (now - START_TIMES.get(name).longValue()) +"ms"); } START_TIMES.put(name, now); } public static synchronized long stop(String name) { if (! START_TIMES.containsKey(name)) { return 0; } return printTimer(name, START_TIMES.remove(name), System.currentTimeMillis()); } private static long printTimer(String name, long start, long end) { LOG.info(name + " timer ran for " + (end - start) + "ms"); return end - start; } } 
+2
Oct 17 '14 at 2:13
source share

The answers still take into account the value of the null key, but the question also asks about any number of null values .

The advantage of storing null with respect to the key in HashMap is the same as in databases, etc. - you can record the difference between having an empty value (for example, the string "") and not have a value at all (null).

+2
Jun 16 '15 at 8:14
source share

Another example: I use it to group data by date. But some data does not have a date. I can group it with the title "NoDate"

+1
Jan 08 '15 at 14:23
source share

A null key can also be useful when the card stores data for user interface selection, where the card key represents the bean field.

The corresponding null field value, for example, will be represented as "(select)" in the user interface selection.

0
Nov 08 '16 at 10:08
source share



All Articles