RuleBasedCollator equals() method in Java with Example

The equals() method of java.text.Collator class is used to check if both the specifiedstrings 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 other than it will return true otherwise false.

Below are the examples to illustrate the equals() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// equals() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator("< a< b< c< d");
  
            // Creating an initializing
            // object for comparison
            String obj1 = "a";
  
            // Creating an initializing
            // Object for comparison
            String obj2 = "A";
  
            // compare both object
            // using equals() mehtod
            boolean i = col.equals(obj1, obj2);
  
            // display result
            if (i)
                System.out.println(obj1
                                   + " is equal to "
                                   + obj2);
            else
                System.out.println(obj1
                                   + " is not equal to "
                                   + obj2);
        }
  
        catch (ParseException e) {
  
            System.out.println(e);
        }
    }
}

chevron_right


Output:

a is not equal to A

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// equals() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator("< a< b< c< d");
  
            // Creating an initializing
            // object for comparison
            String obj1 = "a";
  
            // Creating an initializing
            // Object for comparison
            String obj2 = "a";
  
            // compare both object
            // using equals() mehtod
            boolean i = col.equals(obj1, obj2);
  
            // display result
            if (i)
                System.out.println(obj1
                                   + " is equal to "
                                   + obj2);
            else
                System.out.println(obj1
                                   + " is not equal to "
                                   + obj2);
        }
  
        catch (ParseException e) {
  
            System.out.println(e);
        }
    }
}

chevron_right


Output:

a is equal to a

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/RuleBasedCollator.html#equals-java.lang.Object-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.