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