When to use comparable and comparator interfaces? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

When to use comparable and comparator interfaces?

Both interfaces are implemented when user wants to do sorting for objects of user defined classes.

5th Dec 2016, 2:05 AM
Samrat Indra
Samrat Indra - avatar
1 Answer
0
ImplementingĀ ComparableĀ means "I can compare myself with another object." This is typically useful when there's a single natural default comparison. ImplementingĀ ComparatorĀ means "I can compare two other objects." This is typically useful when there are multiple ways of comparing two instances of a type - e.g. you could compare people by age, name etc.
5th Dec 2016, 7:07 PM
Vipul Walia
Vipul Walia - avatar