What is the best way to check if an object is from another (say, fixed) list of objects?

I am currently creating a HashMap with the object identifier as key and 1 as value. And the method queries Object / Id and checks if there is a corresponding key.

This is normal? Or is there a better alternative (s)?

+4
source share
7 answers

It depends on what you mean by "eat."

If you mean the identifier of the object (i.e. object1 == object2 ), you can use IdentityHashMap as you described.

If you mean object equality (i.e. object1.equals(object2) ), you can simply use a HashSet instead of a monkey using a HashMap.

If your objects use the standard implementations equals() and hashCode() inherited from Object, then this is the difference without a difference: by default they implement the equality of the object as the identifier of the object.

Phill Sacre reminded me of something by suggesting List.contains() . You do not need to use a Set or Map implementation. You can use a List (e.g. ArrayList ). You may find that having a linear shortlist search is less expensive than maintaining a hashed structure.

+10
source

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

Remember that no matter what you do, you must always implement equals () and hashCode () .

+7
source

As others have noted, you can actually use a HashSet for this or any set. If you use your own objects, make sure they override hashCode() (required for HashSet) and equals() (if they override both parameters, they will work with any set).

+5
source

This is normal. Alternatively, you can use a HashSet .

EDIT:

You can compare objects in two ways:

  • Link
  • Custom

By default, Object is compared by reference and implemented in the Object class.

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

Given this, IF , you want to compare objects by reference, but there is a special equality implementation, then you should use IdentityHashMap ELSE use a HashSet .

If you want to keep your current implementation using HashMap , it is also great. HashSet is internally implemented using the HashMap. But instead of setting the value to 1, set it with null .

The question also arises about the correct data structure. You can use List instead of the hash structure. The type of data structure you should use is up to you. It depends on how many people think, how many objects you are going to invest in the collection, how much access will be to them, inserts, etc.

+3
source

This is essentially what a HashSet does, but I would use a HashSet instead of repeating the implementation.

+2
source

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

If you use a HashSet , make sure that you override the implementation of your hashCode() object, used under the hood to verify your identity in this case.

(The last time I burst into JRE, your approach to using HashMap was exactly what HashSet did!)

+1
source

What is the definition of "from another list"? Is this an equality of an object? Then what you have is good, but you can consider a set (hash) for better clarity.

If this is reference equality, you need to look for IdentityHashMap or use the HashSet of IdentityHashcode

+1
source

All Articles