hashCode for byte[]

R

Roedy Green

What algorithm is used for hashCode for byte[]. Where do you look to
see it? Do you have to invent your own wrapper class to get a decent
one?
 
D

Dale King

Roedy Green said:
What algorithm is used for hashCode for byte[]. Where do you look to
see it? Do you have to invent your own wrapper class to get a decent
one?

From section 10.7 in the JLS:

The members of an array type are all of the following:
- The public final field length, which contains the number of components of
the array (length may be positive or zero)
- The public method clone, which overrides the method of the same name in
class Object and throws no checked exceptions
- All the members inherited from class Object; the only method of Object
that is not inherited is its clone method

That says that the it uses the equals and hashCode methods from Object,
which are actually native methods and thus VM dependent but are usually a
permutation of the address of the object itself.

What Sun really needs to do is to define an interface like Comparable for
use with HashMap and HashSet. This interface would implement the methods:

equals( Object a, Object b )
hashCode( Object a )

This would allow you to establish your own criteria for when objects are
equal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top