java.nio.ByteBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。
通过按字典顺序比较两个字节缓冲区的其余元素序列来比较两个字节缓冲区,而不考虑每个序列在其相应缓冲区中的开始位置。对字节元素的比较就好像通过调用Byte.compare(byte,byte)一样。
字节缓冲区无法与任何其他类型的对象进行比较。
用法:
public int compareTo(ByteBuffer that)
参数:此方法将ByteBuffer对象作为参数,将与该缓冲区进行比较。
返回值:由于此缓冲区小于,等于或大于给定的缓冲区,因此此方法返回负整数,零或正整数。
下面是说明compareTo()方法的示例:
范例1:当两个ByteBuffer相等时。
// 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);
// revind 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 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);
// revind 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 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);
// revind 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
相关用法
- Java ByteBuffer get()用法及代码示例
- Java ByteBuffer toString()用法及代码示例
- Java ByteBuffer wrap()用法及代码示例
- Java ByteBuffer getFloat()用法及代码示例
- Java ByteBuffer getDouble()用法及代码示例
- Java ByteBuffer allocate()用法及代码示例
- Java ByteBuffer asCharBuffer()用法及代码示例
- Java ByteBuffer slice()用法及代码示例
- Java ByteBuffer array()用法及代码示例
- Java ByteBuffer arrayOffset()用法及代码示例
- Java ByteBuffer getShort()用法及代码示例
- Java ByteBuffer getInt()用法及代码示例
- Java ByteBuffer compact()用法及代码示例
- Java ByteBuffer asReadOnlyBuffer()用法及代码示例
- Java ByteBuffer asShortBuffer()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ByteBuffer compareTo() method in Java With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。