-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathComparable and Comparator Interface
More file actions
26 lines (19 loc) · 1.01 KB
/
Comparable and Comparator Interface
File metadata and controls
26 lines (19 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Follow the below mentioned Youtube link:
https://www.youtube.com/watch?v=Xy2PqCNgFig
http://www.youtube.com/watch?v=JSvVsOm4oX0
Comparable (compareTo(Object o) method) and Comparator(compare(Object o1, Object o2) and equals(Object o)),
both are interfaces in Java.
Comparator -> To Remember: eater.Hence the method name is compare(Object o1, Object o2)
But for balance it has the second method as well which is equals(Object o)
Comparable -> To Remember: keep the thing. Hence the method is compareTo(Object o). But this is the only method
that it has.
Comparator belongs to util package
Comparable belongs to lang package
Comparator is used for user-defined ordering
Comparable is used for natural ordering
Both are interfaces and support generics:
Interface Comparator<T>
Interface Comparable<T>
Also before going to the video tutorials, please read the java docs for Comparator and Comparable.
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html