Java中的 ByteBuffer compareTo() 方法及示例
Java.nio.ByteBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。
通过按字典顺序比较它们的剩余元素序列来比较两个字节缓冲区,而不考虑每个序列在其相应缓冲区中的起始位置。就像通过调用 Byte.compare(byte, byte) 来比较字节元素对。
字节缓冲区无法与任何其他类型的对象进行比较。
句法 :
public int compareTo(ByteBuffer that)
参数:此方法将 ByteBuffer 对象作为参数,与此缓冲区进行比较。
返回值:此方法返回负整数、零或正整数,因为此缓冲区小于、等于或大于给定缓冲区。
以下是说明compareTo()方法的示例:
示例 1:当两个 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)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);
}
}
}
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);
}
}
}
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);
}
}
}
输出:
ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [20, 30, 40]
both buffer are lexicographically equal
例子2:当这个ByteBuffer大于传递的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);
}
}
}
输出:
ByteBuffer bb: [30, 30, 40]
ByteBuffer bb1: [20, 30, 40]
bb is lexicographically greater than bb1
示例 3:当这个 ByteBuffer 小于传递的 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);
}
}
}
输出:
ByteBuffer bb: [20, 30, 40]
ByteBuffer bb1: [40, 30, 40]
bb is lexicographically less than bb1