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

Discussion

Which one of the following statements about what List.contains uses to decide membership is true?

  • A.The equals method of the elements, not reference identity.
  • B.Reference identity, so an equal but distinct object is not found.
  • C.The hashCode of the elements, exactly as a HashSet does.
  • D.The compareTo method, so the elements must be Comparable.

Answer: A

contains and indexOf both search with equals, so a value-equal object is found even though it is a different instance. That also means a class with no equals override is searchable only by identity, since the inherited version compares references.

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

Post your comment

Please log in or register to post a comment.