Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet that uses the same Comparator?

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The API documentation explains that you need
not override the equals method but that doing so may yield improved performance in some
cases. For example, the addAll method of the TreeSet class can work more effectively if you
add elements from another set that uses the same comparator."

Core Java Vol.I (8th Edition) P.674.

Can you clarify and provide an example?
 
Stephan van Hulst
Bartender
Posts: 6321
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two trees use the same ordering of their elements, adding all of the elements from one tree to another can be made a lot simpler, because a lot of the elements may already be in the correct order, and the tree only has to see which chunks of elements go where.

How can the set tell that another set uses the same ordering? If the comparator it uses equals the comparator of the other set.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic