Collator compare(String, String) method in Java with Example Last Updated : 14 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The compare() method of java.text.Collator class is used to compare the strength of two string and it will return 0, positive and negative values as an output according to the result. Syntax: public abstract int compare(String source, String target) Parameter: This method takes two strings between which comparison is going to take place.Return Value: If the first string is equals, greater, or lesser than the other string then it will return zero, positive and negative values respectively. Below are the examples to illustrate the compare() method: Example 1: Java // Java program to demonstrate // compare() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // Creating and initializing Collator Object Collator col = Collator.getInstance(); // Creating an initializing object for comparison String obj1 = "ab"; // Creating an initializing Object for comparison String obj2 = "Ab"; // compare both object // using compare() method int i = col.compare(obj1, obj2); // display result if (i < 0) System.out.println("ab is less than Ab"); else if (i > 0) System.out.println("ab is greater than Ab"); else System.out.println("ab is equal to Ab"); } catch (ClassCastException e) { System.out.println("Exception thrown : " + e); } } } Output: ab is less than Ab Example 2: Java // Java program to demonstrate // compare() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // Creating and initializing Collator Object Collator col = Collator.getInstance(); // Creating an initializing object for comparison String obj1 = "ab"; // Creating an initializing Object for comparison String obj2 = "cd"; // compare both object // using compare() method int i = col.compare(obj1, obj2); // display result if (i < 0) System.out.println("ab is less than cd"); else if (i > 0) System.out.println("ab is greater than cd"); else System.out.println("ab is equal to cd"); } catch (ClassCastException e) { System.out.println("Exception thrown : " + e); } } } Output: ab is less than cd Reference: https://docs.oracle.com/javase/9/docs/api/java/text/Collator.html#compare-java.lang.String-java.lang.String- Comment More infoAdvertise with us Next Article Collator compare(String, String) method in Java with Example R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-Collator Practice Tags : Java Similar Reads Collator equals(String, String) method in Java with Example The equals() method of java.text.Collator class is used to check if both strings are identical or not. Syntax: public boolean equals(String source, String target) Parameter: This method takes two strings between which comparison is going to take place. Return Value: if both strings are equal to each 2 min read 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 . Syntax: public int compare(Object o1, Object o2) Parameter: This method takes two objects between which comparison 2 min read Collator setStrength() method in Java with Example The setStrength() method of java.text.Collator class is used to set the strength property of Collator object which will help that object during comparison of two string.Syntax: public void setStrength(int newStrength) Parameter: This method accepts the strength property of Collator object as paramet 2 min read Java String compareTo() Method with Examples Strings in Java are objects that are supported internally by an array only which means contiguous allocation of memory for characters. Please note that strings are immutable in Java which means once we create a String object and assign some values to it, we cannot change the content. However, we can 7 min read Boolean compare() method in Java with Examples The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It 2 min read Like