Comparable Interface vs Comparator Interface | Java Tutorial

Published: 15 October 2023
on channel: Java Coding Community
2,522
45

Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

Comparator inteface provides a comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.


On this page of the site you can watch the video online Comparable Interface vs Comparator Interface | Java Tutorial with a duration of hours minute second in good quality, which was uploaded by the user Java Coding Community 15 October 2023, share the link with friends and acquaintances, this video has already been watched 2,522 times on youtube and it was liked by 45 viewers. Enjoy your viewing!