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.
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.