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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。