当前位置: 首页>>代码示例>>用法及示例精选>>正文


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


相关用法

注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ByteBuffer equals() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。