Determine if an object exists in a HashMap by value
Hi,
I'm working on some code that inserts vertices and edges into a map. Prior to my insert, I need to determine if the vertex already exists and if so, I need to use the existing vertex. My first thought was a HashMap, but it only stores the hash of the reference of the Vertex object, so no two will ever be equal. My current working solution is to store the Vertices in an ArrayList and iterate through it on every insert, checking for equality. The problem is, I have a dataset of several million so this takes entirely too long. I'm looking fior suggestions on a better way to approach this. Thanks!
Re: Determine if an object exists in a HashMap by value
This was probably implied, but the JgraphT library I'm using doesn't have an exists() or contains() method that checks by value.
Re: Determine if an object exists in a HashMap by value
If the elements must be unique then the right collection should be a HashSet. In order to work properly the objects stored in the HashSet must override the methods equals() and hashCode(). Note that to calculate the hashCode the formula sould take the same fields used in equals().