Tech
Comparable vs Comparator in Java
In Java, sorting is an important operation when working with collections such as ArrayList, LinkedList, and other data structures. For primitive data types, Java already knows how to sort values. However, for custom objects like Student, Employee, or Product, Java needs instructions on how objects should be compared.
To achieve this, Java provides two interfaces:
Comparable – Used for natural sorting.
Comparator – Used for custom sorting.
Comparable Interface
Comparable is an i...
Read the full discussion on Dev.to
This article was aggregated from Dev.to. Click to join the conversation.
View on Dev.to