CharBuffer compareTo() method in Java
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity1);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
cb.rewind();
System.out.println("Charbuffer cb: "
+ Arrays.toString(cb.array()));
CharBuffer cb1 = CharBuffer.allocate(capacity1);
cb1.put( 'a' );
cb1.put( 'b' );
cb1.put( 'c' );
cb1.rewind();
System.out.println("Charbuffer cb1: "
+ Arrays.toString(cb1.array()));
int i = cb.compareTo(cb1);
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity1);
cb.put( 'g' );
cb.put( 'b' );
cb.put( 'c' );
cb.rewind();
System.out.println("CharBuffer cb: "
+ Arrays.toString(cb.array()));
CharBuffer cb1 = CharBuffer.allocate(capacity1);
cb1.put( 'a' );
cb1.put( 'b' );
cb1.put( 'c' );
cb1.rewind();
System.out.println("CharBuffer cb1: "
+ Arrays.toString(cb1.array()));
int i = cb.compareTo(cb1);
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity1);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
cb.rewind();
System.out.println("CharBuffer cb: "
+ Arrays.toString(cb.array()));
CharBuffer cb1 = CharBuffer.allocate(capacity1);
cb1.put( 'g' );
cb1.put( 'b' );
cb1.put( 'c' );
cb1.rewind();
System.out.println("CharBuffer cb1: "
+ Arrays.toString(cb1.array()));
int i = cb.compareTo(cb1);
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
Share your thoughts in the comments
Please Login to comment...