當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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