Java ByteBuffer compareTo()方法实例

Java ByteBuffer compareTo()方法实例

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);
 
            // 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 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 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程