compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elements are compared as if by invoking Char.compare(char, char). any other type of object can’t be compared to char buffer. Syntax:
public int compareTo(CharBuffer that)
Parameter: This method takes a charbuffer object as a parameter with which this buffer will be compared. Return Value: This method returns a negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer. Below are the examples to illustrate the compareTo() method: Example 1: When both CharBuffer are equal.Â
Java
// Java program to demonstrate// compareTo() methodimport java.nio.*;import java.util.*;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // Declaring the capacity of the cb        int capacity1 = 3;Â
        // Creating the CharBuffer        try {Â
            // creating object of charbuffer cb            // and allocating size capacity            CharBuffer cb = CharBuffer.allocate(capacity1);Â
            // putting the value in cb            cb.put('a');            cb.put('b');            cb.put('c');Â
            // rewind the float buffer            cb.rewind();Â
            // print the Charbuffer            System.out.println("Charbuffer cb: "                               + Arrays.toString(cb.array()));Â
            // creating object of floatbuffer cb1            // and allocating size capacity            CharBuffer cb1 = CharBuffer.allocate(capacity1);Â
            // putting the value in cb1            cb1.put('a');            cb1.put('b');            cb1.put('c');Â
            // rewind the float buffer            cb1.rewind();Â
            // print the Charbuffer            System.out.println("Charbuffer cb1: "                               + Arrays.toString(cb1.array()));Â
            // compare both buffer and store the value into integer            int i = cb.compareTo(cb1);Â
            // if else condition            if (i == 0)                System.out.println("\nboth buffer are"                                   + "lexicographically equal");Â
            else if (i >= 0)                System.out.println("\ncb is lexicographically"                                   + "greater than cb1");Â
            else                System.out.println("\ncb is lexicographically"                                   + "less than cb1");        }Â
        catch (IllegalArgumentException e) {            System.out.println("Exception throws : " + e);        }Â
        catch (ReadOnlyBufferException e) {            System.out.println("Exception throws : " + e);        }    }} |
Charbuffer cb: [a, b, c] Charbuffer cb1: [a, b, c] both buffer arelexicographically equal
Example 2: When this FloatBuffer is greater than the passed FloatBufferÂ
Java
// Java program to demonstrate// compareTo() methodimport java.nio.*;import java.util.*;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // Declaring the capacity of the cb        int capacity1 = 3;Â
        // Creating the CharBuffer        try {Â
            // creating object of floatbuffer cb            // and allocating size capacity            CharBuffer cb = CharBuffer.allocate(capacity1);Â
            // putting the value in cb            cb.put('g');            cb.put('b');            cb.put('c');Â
            // rewind the float buffer            cb.rewind();Â
            // print the CharBuffer            System.out.println("CharBuffer cb: "                               + Arrays.toString(cb.array()));Â
            // creating object of floatbuffer cb1            // and allocating size capacity            CharBuffer cb1 = CharBuffer.allocate(capacity1);Â
            // putting the value in cb1            cb1.put('a');            cb1.put('b');            cb1.put('c');Â
            // rewind the float buffer            cb1.rewind();Â
            // print the CharBuffer            System.out.println("CharBuffer cb1: "                               + Arrays.toString(cb1.array()));Â
            // compare both buffer and store            // the value into integer            int i = cb.compareTo(cb1);Â
            // if else condition            if (i == 0)                System.out.println("\nboth buffer are"                                   + " lexicographically equal");Â
            else if (i >= 0)                System.out.println("\ncb is lexicographically"                                   + " greater than cb1");Â
            else                System.out.println("\ncb is lexicographically"                                   + " less than cb1");        }Â
        catch (IllegalArgumentException e) {            System.out.println("Exception throws : " + e);        }Â
        catch (ReadOnlyBufferException e) {            System.out.println("Exception throws : " + e);        }    }} |
CharBuffer cb: [g, b, c] CharBuffer cb1: [a, b, c] cb is lexicographically greater than cb1
Example 3: When this FloatBuffer is less than the passed FloatBufferÂ
Java
// Java program to demonstrate// compareTo() methodÂ
import java.nio.*;import java.util.*;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // Declaring the capacity of the cb        int capacity1 = 3;Â
        // Creating the CharBuffer        try {Â
            // creating object of CharBuffer cb            // and allocating size capacity            CharBuffer cb = CharBuffer.allocate(capacity1);Â
            // putting the value in cb            cb.put('a');            cb.put('b');            cb.put('c');Â
            // rewind the float buffer            cb.rewind();Â
            // print the CharBuffer            System.out.println("CharBuffer cb: "                               + Arrays.toString(cb.array()));Â
            // creating object of CharBuffer cb1            // and allocating size capacity            CharBuffer cb1 = CharBuffer.allocate(capacity1);Â
            // putting the value in cb1            cb1.put('g');            cb1.put('b');            cb1.put('c');Â
            // rewind the float buffer            cb1.rewind();Â
            // print the CharBuffer            System.out.println("CharBuffer cb1: "                               + Arrays.toString(cb1.array()));Â
            // compare both buffer and store the value into integer            int i = cb.compareTo(cb1);Â
            // if else condition            if (i == 0)                System.out.println("\nboth buffer are"                                   + "lexicographically equal");            else if (i >= 0)                System.out.println("\ncb is lexicographically"                                   + "greater than cb1");            else                System.out.println("\ncb is lexicographically"                                   + "less than cb1");        }Â
        catch (IllegalArgumentException e) {            System.out.println("Exception throws : " + e);        }Â
        catch (ReadOnlyBufferException e) {            System.out.println("Exception throws : " + e);        }    }} |
CharBuffer cb: [a, b, c] CharBuffer cb1: [g, b, c] cb is lexicographicallyless than cb1
