I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
java.lang.Comparable
To implement
Comparable
interface, class must implement a single methodcompareTo()
int a.compareTo(b)
You must modify the class whose instance you want to sort. So that only one sort sequence can be created per class.
java.util.Comparator
To implement Comparator interface, class must implement a single method
compare()
int compare (a,b)
If you see then logical difference between these two is
Comparator
in Java compare two objects provided to him, whileComparable
interface compares "this" reference with the object specified.Comparable
in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implementComparable
interface.If any class implement
Comparable
interface in Java then collection of that object eitherList
orArray
can be sorted automatically by usingCollections.sort()
orArray.sort()
method and object will be sorted based on there natural order defined bycompareTo
method.Objects which implement
Comparable
in Java can be used as keys in a sorted map or elements in a sorted set for exampleTreeSet
, without specifying anyComparator
.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.Comparable
is for objects with a natural ordering. The object itself knows how it is to be ordered.Comparator
is for objects without a natural ordering or when you wish to use a different ordering.here are few differences between Comparator and Comparable I found on web :
If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Array.sort() method and object will be sorted based on there natural order defined by CompareTo method.
Objects which implement Comparable in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.
site:How to use Comparator and Comparable in Java? With example
Read more: How to use Comparator and Comparable in Java? With example
Comparable
is for providing a default ordering on data objects, for example if the data objects have a natural order.A
Comparator
represents the ordering itself for a specific use.