The problem is that two byte[]
use the Object.hashCode
, which tests for the instance of the object. Two array instances as created by new byte[...]
will yield two different hash codes, keys, and hence almost always null is returned.
Furthermore equals
does not work too, hence byte[]
as key is no option.
You could use the string itself, as you are actual doing "key".getBytes(StandardCharsets.UTF_8)
.
Or create wrapper class:
public class ByteArray {
public final byte[] bytes;
public ByteArray(byte[] bytes) {
this.bytes = bytes;
}
@Override
public boolean equals(Object rhs) {
return rhs != null && rhs instanceof ByteArray
&& Arrays.equals(bytes, ((ByteArray)rhs).bytes);
}
@Override
public int hashCode() {
return Arrays.hashCode(bytes);
}
}