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.
Auf dieser Seite können Sie das Online-Video Comparable Interface vs Comparator Interface | Java Tutorial mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Java Coding Community 15 Oktober 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,522 Mal angesehen und es wurde von 45 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!