📜  Java中的 ByteBuffer equals() 方法及示例

📅  最后修改于: 2022-05-13 01:54:19.765000             🧑  作者: Mango

Java中的 ByteBuffer equals() 方法及示例

Java.nio.ByteBuffer类的equals()方法用于检查给定缓冲区是否等于另一个对象。

当且仅当,两个字节缓冲区相等

  • 它们具有相同的元素类型,
  • 它们具有相同数量的剩余元素,并且
  • 剩余元素的两个序列,独立于它们的起始位置考虑,是逐点相等的。

字节缓冲区不等于任何其他类型的对象。

句法:

public boolean equals(Object ob)

参数:此方法将 ob(要与此缓冲区进行比较的对象)作为参数。

返回值:当且仅当此缓冲区等于给定对象时,此方法返回 true。

以下是说明equals()方法的示例:

示例 1:

// Java program to demonstrate
// equals() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer 1
        int capacity1 = 5;
  
        // Declaring the capacity of the  ByteBuffer 2
        int capacity2 = 5;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer 1
            // and allocating size capacity
            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
  
            // creating object of ByteBuffer 2
            // and allocating size capacity
            ByteBuffer bb2 = ByteBuffer.allocate(capacity2);
  
            // putting the int to byte typecast value in ByteBuffer 1
            bb1.put((byte)20);
            bb1.put((byte)30);
            bb1.put((byte)40);
            bb1.rewind();
  
            // putting the value in ByteBuffer 2
            bb2.put((byte)20);
            bb2.put((byte)30);
            bb2.put((byte)40);
            bb2.rewind();
  
            // print the ByteBuffer 1
            System.out.println(" ByteBuffer 1:  "
                               + Arrays.toString(bb1.array()));
  
            // print the ByteBuffer 2
            System.out.println(" ByteBuffer 2:  "
                               + Arrays.toString(bb2.array()));
  
            // checking the equality of both ByteBuffer
            boolean b = bb1.equals(bb2);
  
            // checking if else condition
            if (b)
                System.out.println(" both are equal");
            else
                System.out.println(" both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ByteBuffer 1:  [20, 30, 40, 0, 0]
 ByteBuffer 2:  [20, 30, 40, 0, 0]
 both are equal

示例 2:

// Java program to demonstrate
// equals() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer 1
        int capacity1 = 5;
  
        // Declaring the capacity of the  ByteBuffer 2
        int capacity2 = 3;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer 1
            // and allocating size capacity
            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);
  
            // creating object of ByteBuffer 2
            // and allocating size capacity
            ByteBuffer bb2 = ByteBuffer.allocate(capacity2);
  
            // putting the int to byte typecast value in ByteBuffer 1
            bb1.put((byte)20);
            bb1.put((byte)30);
            bb1.put((byte)40);
            bb1.rewind();
  
            // putting the value in ByteBuffer 2
            bb2.put((byte)20);
            bb2.put((byte)30);
            bb2.put((byte)40);
            bb2.rewind();
  
            // print the ByteBuffer 1
            System.out.println(" ByteBuffer 1:  "
                               + Arrays.toString(bb1.array()));
  
            // print the ByteBuffer 2
            System.out.println(" ByteBuffer 2:  "
                               + Arrays.toString(bb2.array()));
  
            // checking the equality of both ByteBuffer
            boolean b = bb1.equals(bb2);
  
            // checking if else condition
            if (b)
                System.out.println(" both are equal");
            else
                System.out.println(" both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ByteBuffer 1:  [20, 30, 40, 0, 0]
 ByteBuffer 2:  [20, 30, 40]
 both are not equal