Collator compare(Object, Object) method in Java with Example
The compare() method of java.text.Collator class is used to compare the strength of two objects and it will return 0, positive and negative value as an output according to the result .
public int compare(Object o1, Object o2)
Parameter: This method takes two objects between which comparison is going to take place.
Return Value: if the first object is equals, greater or lesser than the other object than it will return zero, positive and negative value respectively.
Exception: This method throws ClassCastException if the arguments cannot be cast to Strings.
Below are the examples to illustrate the compare() method:
ab is less than Ab
Exception thrown : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.