ByteBuffer compareTo() method in Java With Examples
Last Updated :
06 Jun, 2021
The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another.
Two byte buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of byte elements are compared as if by invoking Byte.compare(byte, byte).
A byte buffer is not comparable to any other type of object.
Syntax :
public int compareTo(ByteBuffer that)
Parameter: This method takes a ByteBuffer 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:
Examples 1: When both ByteBuffer 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 bb
int capacity1 = 3;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer bb
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity1);
// putting the byte to int typecast value in bb
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
// rewind the ByteBuffer
bb.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb: "
+ Arrays.toString(bb.array()));
// creating object of ByteBuffer bb1
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
// putting the value in fb1
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
// rewind the ByteBuffer
bb1.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb1: "
+ Arrays.toString(bb1.array()));
// compare both buffer and store the value into integer
int i = bb.compareTo(bb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nbb is lexicographically greater than bb1");
else
System.out.println("\nbb is lexicographically less than bb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output: ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [20, 30, 40]
both buffer are lexicographically equal
Examples 2: When this ByteBuffer is greater than the passed ByteBuffer
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 bb
int capacity1 = 3;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer bb
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity1);
// putting the byte to int typecast value in bb
bb.put((byte)30);
bb.put((byte)30);
bb.put((byte)40);
// rewind the ByteBuffer
bb.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb: "
+ Arrays.toString(bb.array()));
// creating object of ByteBuffer bb1
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
// putting the value in bb1
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
// rewind the ByteBuffer
bb1.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb1: "
+ Arrays.toString(bb1.array()));
// compare both buffer and store the value into integer
int i = bb.compareTo(bb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nbb is lexicographically greater than bb1");
else
System.out.println("\nbb is lexicographically less than bb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output: ByteBuffer bb: [30, 30, 40]
ByteBuffer bb1: [20, 30, 40]
bb is lexicographically greater than bb1
Examples 3: When this ByteBuffer is less than the passed ByteBuffer
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 bb
int capacity1 = 3;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer bb
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity1);
// putting the byte to int typecast value in bb
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
// rewind the ByteBuffer
bb.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb: "
+ Arrays.toString(bb.array()));
// creating object of ByteBuffer bb1
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
// putting the value in fb1
bb1.put((byte)40);
bb1.put((byte)30);
bb1.put((byte)40);
// rewind the ByteBuffer
bb1.rewind();
// print the ByteBuffer
System.out.println("ByteBuffer bb1: "
+ Arrays.toString(bb1.array()));
// compare both buffer and store the value into integer
int i = bb.compareTo(bb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nbb is lexicographically greater than bb1");
else
System.out.println("\nbb is lexicographically less than bb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output: ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [40, 30, 40]
bb is lexicographically less than bb1
Similar Reads
Byte compareTo() method in Java with examples The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax ByteObject.compareTo(Byte a) Parameters: It takes a Byte type object a as input which is to be compared with the ins
2 min read
Byte compare() method in Java with examples The compare() method of Byte class is a built in method in Java which is used to compare two byte values. Syntax Byte.compare(byte a, byte b) Parameters: It takes two byte value 'a' and 'b' as input in the parameters which are to be compared. Return Value: It returns an int value. It returns: 0 if '
2 min read
ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit,
3 min read
ByteBuffer equals() method in Java with Examples The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object. Two byte buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con
3 min read
Boolean compareTo() method in Java with examples The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return
2 min read
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
Calendar compareTo() Method in Java with Examples The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type an
2 min read
Charset compareTo() method in Java with Examples The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second w
1 min read
Boolean compare() method in Java with Examples The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It
2 min read
UUID compareTo() Method in Java with Examples The compareTo() method of UUID class in Java is used to compare one UUID value with another specified UUID. It returns -1 if this UUID is less than the value, 0 if this UUID is equal to the value, and 1 if this UUID is greater than the value. Syntax: UUID_1.compareTo(UUID_2) Parameters: The method t
2 min read