CollationElementIterator reset() method in Java with Examples

The reset() method of java.text.CollationElementIterator class is used to reset the cursor of the iterator to the beginning of the input string.
Syntax:Â
public void reset()
Parameter: This method does not accept any parameter.
Return Value: This method has nothing to return.
Below is the examples to illustrate the reset() method:
Example 1:Â Â
Java
// Java program to demonstrate// reset() methodÂ
import java.text.*;import java.util.*;import java.io.*;Â
public class GFG {    public static void main(String[] argv)    {        // creating and initializing testString        String test = "abcd";Â
        // creating and initializing        // RuleBasedCollator object        RuleBasedCollator rbc            = (RuleBasedCollator)(Collator.getInstance());Â
        // creating and initializing        // CollationElementIterator        CollationElementIterator cel            = rbc.getCollationElementIterator(test);Â
        // setting cursor of iterator        cel.setOffset(2);        cel.next();Â
        // display the result        System.out.println(            "current offset before calling reset() "            + cel.getOffset());Â
        // reset the cursor to the        // beginning of the string        // using reset() method        cel.reset();Â
        // display the result        System.out.println(            "\ncurrent offset after calling reset() "            + cel.getOffset());    }} |
Output:Â
current offset before calling reset() 3 current offset after calling reset() 0
Â
Example 2:Â
Java
// Java program to demonstrate// reset() methodÂ
import java.text.*;import java.util.*;import java.io.*;Â
public class GFG {    public static void main(String[] argv)    {        // creating and initializing        // testString        String test = "abcd";Â
        // creating and initializing        // RuleBasedCollator object        RuleBasedCollator rbc            = (RuleBasedCollator)(Collator.getInstance());Â
        // creating and initializing        // CollationElementIterator        CollationElementIterator cel            = rbc.getCollationElementIterator(test);Â
        // setting cursor of iterator        cel.setOffset(3);        cel.previous();Â
        // display the result        System.out.println(            "current offset before calling reset() "            + cel.getOffset());Â
        // reset the cursor to the beginning of the string        // using reset() method        cel.reset();Â
        // display the result        System.out.println(            "\ncurrent offset after calling reset() "            + cel.getOffset());    }} |
Output:Â
current offset before calling reset() 2 current offset after calling reset() 0
Â
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/CollationElementIterator.html#reset–
Â



