Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a Comparator in Collections

 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I have a doubt in this code, where is the compare method called in the CompDemo class ??
The output is F E D C B A.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From lines 19 through 24. Since you have specified a comparator, the compare() method is called when you add each element to the TreeSet.

Read -> http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#TreeSet(java.util.Comparator)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By compareTo() I presume you mean compare()?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting that Campbell... i have edited my initial reply..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic