COMPARABLE VS COMPARATOR
.png)
In this article, I will be explaining why java came up with COMPARATOR in the latest versions even though it already has a COMPARABLE interface You can find small code snippets along the way for better understanding. Let's dive into the article Whenever we are designing the class, we will have some idea of what variables we need to store as part of this class Similarly, if we also know how to sort that object then we will implement the Comparable interface and we will override the compareTo method In that compareTo method, we will be writing the sort logic whether it should return 1, -1, or 0, and based on that we will sort elements in the objects class Movie implements Comparable < Movie >{ private int id; private String name; private int rating; @Override public int compareTo (Movie o) { if ( this .rating < o.rating) return - 1 ; if ( this .rating > o.rating) retu...