Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Collator getStrength() method in Java with Example

  • Last Updated : 19 Aug, 2021

The getStrength() method of java.text.Collator class is used to get the strength property of Collator object which will help that object during comparison.

Syntax: 

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.

public int getStrength()

Parameter: This method does not accept any parameter.
Return Value: This method returns the strength property of the Collator object which will help that object during comparison.

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



Example 1:  

Java




// Java program to demonstrate
// getStrength() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing
            // new simple rule
            String simple
                = "< a < b < c < d";
 
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
 
            // setting strength property
            // for the Collator object
            col.setStrength(Collator.IDENTICAL);
 
            // getting strength property
            // using getStrength() method
            int strength = col.getStrength();
 
            // display result
            if (strength == 0)
                System.out.println(
                    "current strength "
                    + "property :- PRIMARY.");
            else if (strength == 1)
                System.out.println(
                    "current strength "
                    + "property :- SECONDARY.");
            else if (strength == 2)
                System.out.println(
                    "current strength"
                    + " property :-  TERTIARY.");
            else
                System.out.println(
                    "current strength "
                    + "property :- IDENTICAL.");
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ParseException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output: 
current strength property :- IDENTICAL.

 

Example 2: 

Java




// Java program to demonstrate
// getStrength() 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(Locale.UK);
 
            // getting strength property
            // using getStrength() method
            int strength = col.getStrength();
 
            // display result
            if (strength == 0)
                System.out.println(
                    "current strength "
                    + "property :- PRIMARY.");
            else if (strength == 1)
                System.out.println(
                    "current strength "
                    + "property :- SECONDARY.");
            else if (strength == 2)
                System.out.println(
                    "current strength "
                    + "property :-  TERTIARY.");
            else
                System.out.println(
                    "current strength "
                    + "property :- IDENTICAL.");
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output: 
current strength property :-  TERTIARY.

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/Collator.html#getStrength–
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :