1.

What is a Comparator in java?

Answer»

Consider the example where we have an ArrayList of employees LIKE( EId, Ename, Salary), etc. Now if we want to sort this list of employees based on the NAMES of employees. Then that is not possible to sort USING the Collections.sort() METHOD. We need to PROVIDE something to the sort() function depending on what values we have to perform sorting. Then in that case a comparator is used.

Comparator is the interface in java that contains the compare method. And by overloading the compare method, we can define that on what basis we need to compare the values. 



Discussion

No Comment Found