comparator interface

i would like to know more about comparator interface. why it is in java.util not in java.lang.

how it is different from comparable interface.

how compare() different from compareTo().

why implementing comparator second method equals() is optional unlike implementing compare() is mandotory
please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Margaret GreenCommented:
1. Comparable interface in java.lang is used to define the natural ordering on a class. A class can implement it only once. Comparator in java.util defines a total ordering on instances of a class. A Comparator implementing class can be separate class from the class it orders. A developer can create multiple Comparators for a class. That is, multiple orderings of of collection can be defined with separate Comparator implementations.

2. The Comparable interface method, compareTo() should throw a NullPointerException when null is given as the input. The Comparator interface compare() can accept a null value.

3. Comparable is implemented by a class that has a natural ordering among its member instances. Comparator is implemented to provide a total ordering on a class that may not have a natural ordering. There is only one natural ordering on instances of a class, the can be more than one total ordering of instances of a class.

4. Comparable should be consistent with equals. The equals method is specified by the class implementing the Comparable interface. A Comparator may be implemented by the class it defines a total ordering for, but it may also be implemented by some other class for the purpose of defining a total ordering on a target class. In the latter case, equals should be implemented because it will be comparing whether two Comparator instances are equal, that is they impose the the same total ordering.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.