Related Articles

Related Articles

Collator setStrength() method in Java with Example
  • Last Updated : 03 Jan, 2020

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 parameter which will help this object during the comparison.

Return Value: This method has nothing to return.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setStrength() 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
            // using getStrength() mehtod
            col.setStrength(Collator.IDENTICAL);
  
            // getting strength property
            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);
        }
    }
}

chevron_right


Output:

current strength property :- IDENTICAL.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setStrength() 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);
  
            // setting strength property
            // for the Collator object
            // using getStrength() mehtod
            col.setStrength(Collator.PRIMARY);
  
            // getting strength property
            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);
        }
    }
}

chevron_right


Output:

current strength property :- PRIMARY.

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/Collator.html#setStrength-int-

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :