ByteBuffer hashCode() method in Java with Examples
The hashCode() method of java.nio.ByteBuffer class is used to return the current hash code of this buffer. The hash code of a byte buffer depends only upon its remaining elements; that is, upon the elements from position() up to, and including, the element at limit() – 1. Because buffer hash codes are content-dependent, it is inadvisable to use buffers as keys in hash maps or similar data structures unless it is known that their contents will not change.
Syntax:
public int hashCode()
Return Value: This method returns the current hash code of this buffer. Below are the examples to illustrate the hashCode() method: Examples 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ByteBuffer bb = ByteBuffer.allocate( 12 );
bb.asIntBuffer()
.put( 10 )
.put( 20 )
.put( 30 );
bb.rewind();
System.out.println("Original ByteBuffer: ");
for ( int i = 1 ; i <= 3 ; i++)
System.out.print(bb.getInt() + " ");
bb.rewind();
int value = bb.hashCode();
System.out.println("\n\nByte Value: " + value);
}
}
|
Output:
Original ByteBuffer:
10 20 30
Byte Value: -219122491
Examples 2:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ByteBuffer bb = ByteBuffer.allocate( 12 );
int value = bb.hashCode();
System.out.println("Byte Value: " + value);
}
}
|
Output:
Byte Value: -293403007
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#hashCode–
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...