Saral Saxena wrote:Hi,
I am going through comparator and comparable, I have done lot of googling also, can you guys please explain in simple,very simple words that whats the use of both of them In which real life situation we should choose comparable and in which situation we should go for comparator..!! please if possible explain it with a small example..!!
Well, things are simple:
- Comparator: A comparator compares two object together by the method: compare(Object1, Object2). The objects being compared do not have to aware about the comparison logic. This is suitable for the situation in which you don't want to modify the compared object class.
- Comparable: A comparable object must implement the compareTo(Object) method, the compared object is responsible for the comparison logic. In this way you have to write additional code for the compared objects.