Open In App

CharBuffer compareTo() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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() 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 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);
        }
    }
}


Output:

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() 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 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);
        }
    }
}


Output:

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);
        }
    }
}


Output:

CharBuffer cb: [a, b, c]
CharBuffer cb1: [g, b, c]

cb is lexicographicallyless than cb1


Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads