> >Did you read the API docs?
> >Do you know what Comparator and Comparable are used
> for?
>
> They are used for sorting. Collections.sort()
> requires the class whose objects have to be sorted to
> implement any of these interfaces.
There was an armed robbery last week, and the thieves used a car to escape. So can we safely define "car" as "a mode of transport employed by armed robbers"?
>Did you read the API docs?
> >Do you know what Comparator and Comparable are used
> for?
>
> They are used for sorting. Collections.sort()
> requires the class whose objects have to be sorted to
> implement any of these interfaces.
>There was an armed robbery last week, and the thieves used a car to escape.
>So can we safely define "car" as "a mode of transport employed by armed robbers"?
No, definitely cant....i meant that's the case what i came accross of them...i would like to know how do they differ funtionally...
> >Did you read the API docs?
> > >Do you know what Comparator and Comparable are
> used
> > for?
> >
> > They are used for sorting. Collections.sort()
> > requires the class whose objects have to be sorted
> to
> > implement any of these interfaces.
>
> >There was an armed robbery last week, and the
> thieves used a car to escape.
> >So can we safely define "car" as "a mode of
> transport employed by armed robbers"?
>
> No, definitely cant....i meant that's the case what i
> came accross of them...i would like to know how do
> they differ funtionally...
Don't their respective signatures give you any clue?
It's probably not put formally correct.
Comparable: something that can be compared -> sets the default (natural) way of comparing/ordering
Comparator: something that can compare -> can be used in case multiple different ways are required to compare 2 objects of some type. So when you call compare, you use an instance of the class that implements the comparator...
> compare() has both the objects as arguments which are
> needed to be compared....so through which object i
> should call it....
The compare() method should be called on the instance of Comparator, and passed references to the two objects you wish to compare. Alternately, you can pass the Comparator to existing utility methods, whereby you won't be calling compare() directly; e.g., Collections.sort().
Go through the tutorial again. It answers your questions and give you examples.
~