Results 1 to 3 of 3
  1. #1
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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!

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  3. #3
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default 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().
    Last edited by javaPower; 10-29-2011 at 04:08 PM.

Similar Threads

  1. Replies: 6
    Last Post: 10-11-2010, 01:59 PM
  2. How do I check if a database exists ...Any help?
    By nmvictor in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 04:21 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  5. Check if a web page exists or not
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •