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

Discussion

Whether a collection may contain itself as an element - which of the following is true?

  • A.It is permitted and fully supported, with self-reference handled by every method.
  • B.It is permitted only for a Set, whose duplicate check prevents recursion.
  • C.It is permitted for some implementations, but hashCode and toString may then recurse without end.
  • D.It is impossible, because add rejects the collection itself.

Answer: C

Nothing stops a List holding a reference to itself, and the javadoc warns that behaviour is undefined for such collections: hashCode, equals and toString are defined recursively over the elements and will not terminate. It is a curiosity rather than a technique.

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

Post your comment

Please log in or register to post a comment.