Tag: comparator

What are advantages of comparator in Java over comparable?

Answer includes advantages of comparator over comparable interfaces and difference between both in Java collections. Note that advantages  of comparator over coparable and when would you use comparable and comparator interface is a frequently asked java interview question. You may read what is comparable and comparator interface in java collections before reading difference and advantages. Answer: Comparable Vs Comparator…

Read the full article

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…

Read the full article