Comparable Interface vs Comparator Interface | Java Tutorial

Pubblicato il: 15 ottobre 2023
sul canale di: 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.


In questa pagina del sito puoi guardare il video online Comparable Interface vs Comparator Interface | Java Tutorial della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Java Coding Community 15 ottobre 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 2,522 volte e gli è piaciuto 45 spettatori. Buona visione!