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

Discussion

Which of these statements about using an array as an element of a HashSet is correct?

  • A.Two distinct arrays with identical contents count as duplicates.
  • B.Arrays cannot be stored in a HashSet; the add is rejected at compile time.
  • C.Arrays are stored using deep equality.
  • D.Two distinct arrays with identical contents are both stored.

Answer: D

An array inherits identity equality and an identity hash code, so two arrays are the same only when they are the same object. A set of arrays therefore almost never behaves as intended; a List, or a wrapper class with proper equals and hashCode, is what is wanted.

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

Post your comment

Please log in or register to post a comment.