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 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
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