Java Interview Question #5: What is difference between Comparator and Comparable in Java

Published: 31 July 2016
on channel: JavaRadio
126
1

a. Both interfaces are used during shorting
b. The object who is being compared implements Comparable
c. The object who compares or by using which comparison happens- implements Comparator.
d. Comparable – int compareTo() and Comparator – int compare()
e. Comparable:
======================
i. Collections.sort(empList);
ii. Arrays.sort(empArray);
Note: Employee object need to implement Comparable interface

f. Comparator:
================
i. Collections.sort(empList, new SortEmployeeById());
ii. Arrays.sort(empArray, new SortEmployeeByName());
Note: SortEmployeeById, SortEmployeeByName need to implement Comparator interface

g. More info:http://www.beingjavaguys.com/2013/10/...


On this page of the site you can watch the video online Java Interview Question #5: What is difference between Comparator and Comparable in Java with a duration of hours minute second in good quality, which was uploaded by the user JavaRadio 31 July 2016, share the link with friends and acquaintances, this video has already been watched 126 times on youtube and it was liked by 1 viewers. Enjoy your viewing!