views:

834

answers:

7

Currently, I create a HashMap with Object Id as key and 1 as value. And the method asks for Object/Id and checks if there is a matching key.

Is that ok? Or, is(are) there better alternative(s)?

+2  A: 

That's ok. Alternately you could use a HashSet.

EDIT:

You can compare objects in 2 ways:

  1. By Reference
  2. Custom

The Default Object comparison is done by reference and it's implemented on the Object class.

If any class in the hierarchy of your object overrides this default implementation then your doing a custom comparison. If this happens, you must also override the hashcode as well.

Given this, IF you want to compare objects by reference but there's a custom implementation of equality then you should use a IdentityHashMap ELSE use a HashSet.

If you want to maintain your current implementation with HashMap it's also fine. HashSet is internally implemented with an HashMap. But, instead of setting the value to 1, set it with null.

There's also the question of the correct data structure. You could use a List instead of an Hash structure. The type of data structure you should use is up to you. This depends on many thinks like how many objects you intend to put in the collection, how many accesses there will be, insertions, etc.

bruno conde
+6  A: 

Would List.contains(Object) do what you want?

Remember though, whatever you do you should always implement equals() and hashCode().

Phill Sacre
+2  A: 

That is essentially what HashSet does, but I'd use HashSet instead of repeating the implementation.

Stephen Denne
+1  A: 

As Bruno suggests you can use a set for your fixed list of objects, and then call contains().

If you use a HashSet, make sure you're overriding your object's hashCode() implementation which is used under the hood for identity checking in this case.

(Last time I dug into the JRE, your approach of using a HashMap was exactly what a HashSet did anyway!)

Dan Vinton
+1  A: 

What is the definition of "is from another list"? Is it the object equality? Then what you have is good, but you might consider a (Hash)Set for better clarity.

If it is reference equality you need then look up IdentityHashMap, or use a HashSet of IdentityHashcode

Hemal Pandya
+9  A: 

It depends on what you mean by "is".

If you mean object identity (i.e. object1 == object2) then you could use an IdentityHashMap in the way you've described.

If you mean object equality (i.e. object1.equals(object2)) then you can just use a HashSet instead of a monkeying around with a HashMap.

If your objects use the default implementations of equals() and hashCode() inherited from Object, then this is a distinction with no difference: the defaults implement object equality as object identity.

Phill Sacre reminded me of something by suggesting List.contains(). You don't have to use a Set or Map implementation. You could use a List (e.g. ArrayList).You might find that having contains perform a linear search through a short list is less costly than maintaining a hashed structure.

bendin
Note that HashSet is really just a wrapper around a HashMap with null values, so the only difference is that the intent is clearer.
Michael Borgwardt
+4  A: 

As others have noted, you could use a HashSet for that, or any sort of Set, actually. If you use your own objects, make sure they override hashCode() (required for HashSet) and equals() (if they override both, they will work with any Set).

Fabian Steeg