Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these statements about null keys and values in a ConcurrentHashMap is correct?

  • A.One null key and any number of null values are permitted, as in HashMap.
  • B.Null values are permitted but a null key is not.
  • C.Null keys are permitted but a null value is not.
  • D.Neither a null key nor a null value is permitted; both throw NullPointerException.

Answer: D

Unlike HashMap it rejects both. The reason is ambiguity under concurrency: with null values a lookup returning null could not distinguish an absent key from a present one mapped to null, and no lock is held to check afterwards. Migrating a HashMap that stored nulls therefore needs care.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.