ByteBuffer hashCode() method in Java with Examples
source link: https://www.geeksforgeeks.org/bytebuffer-hashcode-method-in-java-with-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ByteBuffer hashCode() method in Java with Examples
- Last Updated : 17 Jun, 2019
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 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
Examples 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
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#hashCode–
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK