Why are ByteBuffers hashCodes the same?

I have a class like this:

public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size()); buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN); setByteBuffer(buffer, 0); System.out.println("buffer.hasCode=" + buffer.hashCode()); } 

In my other classes, I create many instances of the above class in different places and times using

 new JavoImageCorrectedDataHeader() 

Then I expected it to print a different hash code for them. but actually I see that the same hashCode prints:

 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 buffer.hasCode=1742602241 

I need to skip something about how to use ByteBuffer.

+6
source share
4 answers

From javadoc:

The byte buffer hash only depends on the rest of the elements; that is, on elements from position () to and inclusive, the element in the limit () is 1.

Since buffer hash codes are content dependent, it is not practical to use buffers as keys in hash maps or similar data structures unless it is known that their contents will not change.

If you don't fill in ByteBuffers or don't fill them with the same things, the hash codes will be identical.

+10
source

From ByteBuffer.java source code:

 public int hashCode () { int hashCode = get(position()) + 31; int multiplier = 1; for (int i = position() + 1; i < limit(); ++i) { multiplier *= 31; hashCode += (get(i) + 30)*multiplier; } return hashCode; } 

In your current implementation, position() always returns 0 , and so the hash codes are always the same. The hash code depends on the contents of the buffer, and not on the physical object that is used to represent it.

+5
source

This is the correct behavior. In the ByteBuffer documentation:

Two byte buffers are equal if and only if

They have the same element type,

They have the same number of remaining elements, and

The two sequences of the remaining elements, read regardless of their initial positions, are pointwise equal.

The byte buffer is not equal to any other type of object.

So, assuming this.size () always returns the same thing, your buffers are always equal. According to the general hashCode contract, they must have the same hash code.

It seems you are trying to use hashCode to determine the identity of an object - this is not a good idea (due to the way hashCode and == interact. If you need to distinguish between instances of your class from each other and you need more than the == operator gives you, you will have to find another way to do this.

+3
source

ByteBuffer.hashcode allows ByteBuffer.hashcode to calculate the hash of the wrapped byte []. In this case, the contents of the newly initialized byte [] is 0 for each byte. Given that the contents of the ByteBuffer are the same, the hash code is the same.

+2
source

Source: https://habr.com/ru/post/918399/


All Articles