Comparable Interface vs Comparator Interface | Java Tutorial

Publié le: 15 octobre 2023
sur la chaîne: 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.


Sur cette page du site, vous pouvez voir la vidéo en ligne Comparable Interface vs Comparator Interface | Java Tutorial durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Java Coding Community 15 octobre 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 2,522 fois et il a aimé 45 téléspectateurs. Bon visionnage!