0

I'd like to be able to determine whether I've encountered an object before - I have a graph implementation and I want to see if I've created a cycle, probably by iterating through the Node objects with a tortoise/hare floyd algorithm.

But I want to avoid a linear search through my list of "seen" nodes each time. This would be great if I had a hash table for just keys. Can I somehow hash an object? Aren't java objects just references to places in memory anyway? I wonder how much of a problem collisions would be if so..

4 Answers 4

5

The simple answer is to create a HashSet and add each node to the set the first time you encounter it.

The only case that this won't work is if you've overloaded hashCode() and equals(Object) for the node class to implement equality based on node contents (or whatever). Then you'll need to:

  • use the IdentityHashMap class which uses == and System.identityHashcode rather than equals(Object) and hashCode(), or
  • build a hashtable yourself using your own flavour of object identity.

Aren't java objects just references to places in memory anyway?

Yes and no. Yes, the reference is represented by a memory address (on most JVMs). The problem is that 1) you can't get hold of the address, and 2) it can change when the GC relocates the object. This means that you can't use the object address as a hashcode.

The identityHashCode method deals this by returning a value that is initially based on the memory address. If you then call identityHashCode again for the same object, you are guaranteed to get the same value as before ... even if the object has been relocated.

I wonder how much of a problem collisions would be if so..

The hash values produced by the identityHashCode method can collide. (That is, two distinct objects can have the same identity hashcode value.) Anything that uses these values has to deal with this. (The standard HashSet and IdentityHashMap classes take care of these collisions ... if you chose to use them.)

Sign up to request clarification or add additional context in comments.

Comments

4

I'd like to be able to determine whether I've encountered an object before

Use an IdentityHashMap. It is the ideal for your job since it is not an equals but a == implementation.

1 Comment

The reference is dead :-(
2

Take a look at HashSet. Note that in order for objects to work with HashSet, they need to provide correct implementations of hashCode and equals methods of the java.lang.Object class.

1 Comment

It's likely that the default implementations of hashCode() and equals(), which have identity semantics, will be sufficient for the OP.
1

You'll need to implement a hash function for your objects. This is done by overriding hashCode() defined in java.lang.Object. This method is used by HashMap, HashSet etc to store objects. In hashCode() it's up to you to calculate a hash for the object. Don't forget to also implement the equals()-method!

Take a look at Java collection framework (http://docs.oracle.com/javase/tutorial/collections/)

2 Comments

Your first sentence is misleading: there is a built-in hashCode() that provides identity semantics.
Thank you @kdgregory. First sentence removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.