What are advantages of comparator in Java over comparable?

Answer includes advantages of comparator over comparable interfaces in java and the differences between them. As a side note, what’re advantages  of comparator over comparable, and when would you use comparable and comparator interface is a frequently asked interview question for java developer profile. You may read what is comparable and comparator interface in java collections before reading the […]

Write a program to sort employees by their experience. Employee class is not allowed to implement any interface.

Answer: For sorting employees we need to use either Comparable or comparator interface. Since, as per question, the Employee class is not allowed to implement any Interface, only option is, to create a new comparator class that implements Comparator interface. And in “compare()” method of comparator class, write the comparison code for the employee class […]

Scroll to top