filmov
tv
Java comparable vs comparator (manohar academy)
![preview_player](https://i.ytimg.com/vi/lEJxykLZNvw/maxresdefault.jpg)
Показать описание
welcome to Manohar Academy.
In this lesson we compare and contrast comprable with comprator.
Similarities:
Provide sorting order
Have same contract on the returned integer
orderings is recommended to be consistent with equals
both are part of collection framework
Differences:
Comparable deals with natural sorting order.
Comparator provides custom sorting order
With comparable only one sorting order is possible.
With comparator you can create many custom sorting orders.
Comparable is implemented by a class whose objects need to be sorted.
Comparator is mostly implemented by a different class.
The abstract method in comparable is compareTo and it takes only one argument.
The abstract method in comprator is compare and it takes two arguments.
The equals method in class implementing the comparable checks equality of objects that are being sorted.
The equals method in class implementing comparator checks whether to comparator objects are sorting same or not.
There is no recommendation on serializability of the class implementing comparable.
The class implementing comparator is recommended to be serializable.
In this lesson we compare and contrast comprable with comprator.
Similarities:
Provide sorting order
Have same contract on the returned integer
orderings is recommended to be consistent with equals
both are part of collection framework
Differences:
Comparable deals with natural sorting order.
Comparator provides custom sorting order
With comparable only one sorting order is possible.
With comparator you can create many custom sorting orders.
Comparable is implemented by a class whose objects need to be sorted.
Comparator is mostly implemented by a different class.
The abstract method in comparable is compareTo and it takes only one argument.
The abstract method in comprator is compare and it takes two arguments.
The equals method in class implementing the comparable checks equality of objects that are being sorted.
The equals method in class implementing comparator checks whether to comparator objects are sorting same or not.
There is no recommendation on serializability of the class implementing comparable.
The class implementing comparator is recommended to be serializable.