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

前端 未结 7 1416
挽巷
挽巷 2020-12-12 16:19

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

7条回答
  •  青春惊慌失措
    2020-12-12 16:29

    The answers so far only consider the worth of have a null key, but the question also asks about any number of null values.

    The benefit of storing the value null against a key in a HashMap is the same as in databases, etc - you can record a distinction between having a value that is empty (e.g. string ""), and not having a value at all (null).

提交回复
热议问题