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.

12/5/2016 2:05:18 AM

Samrat Indra

1 Answer

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