Results 1 to 3 of 3
  1. #1
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Question Efficient Bi-Directional Mapping Between Object References and IDs?

    Hi,

    I want to associate int IDs to object references for arbitrary Object instances (so by design I cannot store the ID as a new field of the Object instances). I am trying to build a bi-directional map that allows to search for an object reference logarithmically given an ID and then to search for an ID logarithmically given an object reference. To do the former, it is relatively easy, I just need to define the class

    private class element {
    private int id;
    private Object reference;
    }

    , then populate an array element[] with objects sorted by element.id and then use binary search for a given id. The problem I have is that I cannot find any way to do the search efficiently in the other direction. I cannot simply define another array element[] using the same element instances but this time sorted using element.reference, since I cannot find a way to sort references in Java. For this particular case calling the hashing function of my objects would be much less efficient obviously and would not even insure that I am returning the id corresponding to the right object. So I would like to know is there anything I am missing or any better way to do this in Java? Is there in particular some method in the API that allows somehow to indirectly and surely compare object references, or that performs the object reference sorting internally (for example in its native code) and that I could reuse?

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,405
    Rep Power
    20

    Default

    Is there in particular some method in the API that allows somehow to indirectly and surely compare object references
    huh? == directly compares references for equality. No method required.

    And if you must have a method and wrap the reference in an Element (not element) class, you can override equals(...) to do that.
    Java Code:
    public boolean equals(Object other) {
       if (other == null) {
          return false;
       }
       if (!(other instanceof Element) {
          return false;
       }
       return reference == ((Element) other).reference;
    }
    If you do this, don't forget to override hashCode() in accordance with the contract for hashCode() and equals().

    db

    db

  3. #3
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    huh? == directly compares references for equality. No method required.
    By comparing I mean the equivalent to < and > operators of course (to be able to perform a binary search). == would only allow to loop over all the elements until the right one is found, which is a linear, not logarithmic algorithm...

Similar Threads

  1. Confused About Object References
    By jtmoney0511 in forum New To Java
    Replies: 16
    Last Post: 10-18-2009, 01:15 AM
  2. Multiple references to the same object
    By jlowery2663 in forum Advanced Java
    Replies: 6
    Last Post: 08-31-2009, 07:40 AM

Posting Permissions

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