"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by Naseem Khan:
The advantage which Comparator give sover Comparable is you can sort the same collection in n no. of ways by passing different comparators in each case.
Naseem
Originally posted by pramila ch:
The elements in TreeSet/TreeMap should implement comparable i/f by default.
Should they implement either comparable or comparator interface?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by pramila ch:
...Can we implement comparable interface and override compareTo method to provide different ways of comparing objects other than natural ordering? ...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by pramila ch:
Can we implement comparable interface and override compareTo method to provide different ways of comparing objects other than natural ordering? then wouldn't it be the same kind of service provided by comparator?
Originally posted by pramila ch:
If we want to sort objects in different ways, for eg:
1. sort the string objects in TreeSet using alphabetical order
2. sort the string objects in the same TreeSet using case insensitive alphabetical order
then objects in the same TreeSet can't have different implementation to same compareTo method. hence we do use comparator for second way of sorting.
The point which is bugging me is why can't the TreeSet/Map take comparable interface itself as the parameter in constructor ? what's the need to have comparator interface separately?
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|