Lookup Java non-String keyed Java HashMap with string -
i'm trying have hashmap keyed circuitid class. circuitid class contains string clci member , class overrides hashcode()/equals() using clci member's hashcode()/equals().
i wanted can lookup map using simple string , away converting string circuitid object lookup map. doesn't work , think because hashmap uses evaluation form (key==null ? k==null : key.equals(k)) key input key , k key entry in map, key.equals(k) part. i'm wondering why hashmap didn't k.equals(key) instead?
(at least way, think i'm trying have worked.) being that's not case, there trick this?
if have overridden equals() check handle if key being passed string or actual circuitid object, don't understand why wouldn't have worked.
whether container compares circuit string, or string circuit implementation-specific, , not guaranteed same in future releases of jre.
generally, given 2 objects a , b, expected a.equals(b) when b.equals(a) , vice-versa. called symmetric property.
in case of circuitid , string, while may override circuitid.equals() , make circuit.equals(id) return true, id.equals(circuit) false because cannot override string.equals().
you should use map<string,circuitid> (and perhaps map<circuitid,somethingelse> in case need have circuitid key in other map.
Comments
Post a Comment