How is this possible:
HashMap<byte[], byte[]> and what is hash() of byte[]?
How is this possible: HashMap<byte[], byte[]> and what is hash() of byte[]?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it is possible (with a big caveat, see below), but
byte[]is not an “intrinsic type”. First, there’s no such thing, you probably mean a “primitive type”. Second:byte[]is not a primitive type,byteis. An array is always a reference type.Arrays don’t have specific
hashCodeimplementations, so they’ll just use thehashCodeofObject, which means that thehashCodewill be the indentity-hashCode, which is independent from the actual content.In other words: a
byte[]is a very badMapkey, because you can only retrieve the value with the exact same instance.If you need a content-based
hashCode()based on an array, you can useArrays.hashCode(), but that won’t help you (directly) with theMap. There’s alsoArrays.equals()to check for content equality.You could wrap your
byte[]in a thin wrapper object that implementshashCode()andequals()(using the methods mentioned above):Using this class you can then use a
Map<ArrayWrapper,byte[]>.