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

Discussion

Which one of the following statements about which method decides whether two keys are the same in a HashMap is true?

  • A.hashCode selects the bucket, then equals compares the keys within it.
  • B.equals alone, applied to every key currently in the map.
  • C.compareTo, so keys must implement Comparable.
  • D.Reference identity, which is why keys are usually interned strings.

Answer: A

Both methods are involved and both must be correct: a key whose hashCode disagrees with equals lands in a bucket where the lookup never looks. This is the same requirement HashSet has, since a HashSet is a HashMap of keys.

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

Post your comment

Please log in or register to post a comment.