When to use comparable and comparator interfaces? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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 ответ
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