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


Java ByteBuffer hashCode()用法及代碼示例


java.nio.ByteBuffer類的hashCode()方法用於返回此緩衝區的當前哈希碼。
字節緩衝區的哈希碼僅取決於其剩餘元素;也就是說,從position()到(包括)limit()處的元素– 1。
因為緩衝區哈希碼是content-dependent,所以不建議將緩衝區用作哈希映射或類似數據結構中的鍵,除非已知緩衝區的內容不會改變。

用法:

public int hashCode()

返回值:此方法返回此緩衝區的當前哈希碼。


下麵是說明hashCode()方法的示例:

範例1:

// Java program to demonstrate 
// getInt() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // putting the int value in the bytebuffer 
        bb.asIntBuffer() 
            .put(10) 
            .put(20) 
            .put(30); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // print the ByteBuffer 
        System.out.println("Original ByteBuffer: "); 
        for (int i = 1; i <= 3; i++) 
            System.out.print(bb.getInt() + " "); 
  
        // rewind the Bytebuffer 
        bb.rewind(); 
  
        // Reads the Int at this buffer's current position 
        // using getInt() method 
        int value = bb.hashCode(); 
  
        // print the int value 
        System.out.println("\n\nByte Value: " + value); 
    } 
}
輸出:
Original ByteBuffer: 
10 20 30 

Byte Value: -219122491

範例2:

// Java program to demonstrate 
// getInt() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // creating object of ByteBuffer 
        // and allocating size capacity 
        ByteBuffer bb = ByteBuffer.allocate(12); 
  
        // Reads the Int at this buffer's current position 
        // using getInt() method 
        int value = bb.hashCode(); 
  
        // print the int value 
        System.out.println("Byte Value: " + value); 
    } 
}
輸出:
Byte Value: -293403007

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#hashCode–



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer hashCode() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。