• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is comparator in sorted set  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the document

http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#comparator()

But I am not able to get what is actual use of this in set ?


Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#comparator()

But I am not able to get what is actual use of this in set ?


A comparator is something whose purpose is to compare two elements. It is passed to the sorted set, for cases where you want the sorted set to sort elements which are not comparable. Or if you want the elements sorted in a fashion that is different from the natural ordering provided by the comparable elements.

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any example using code?

can you explain
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:any example using code?

can you explain


Hi Kishor. Comparators can be a bit tricky to understand. I found this link very helpful, give it a read http://javahash.com/java-comparator-and-comparable-demystified/
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One part of the Java Tutorials you might do well to read is about Collections. Read it very quickly and you will forget 99% of what you read. No problem: you can always go back and read it again.

As you go through it, you will come across a section about object ordering. That tells you about Comparators.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks campbell

I will go for these links
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!