Re: How can i implements a class like an HashMap but with 2 keys ?



Rob wrote:
i [sic] want to create a class that works like an HashMap but
with 2 keys, get(key1,key2) -> value
I was thinking to create a class "Pair"
that override equals and hashcode [sic]

That's "hashCode".

and then store here the two keys and then a class that holds an HashMap
with Pair as key and the value class, can be this a solution?

This would be the canonical solution. Make Pair immutable and generic if your keys are of general type, non-generic if you know what the key types are.

--
Lew
.



Relevant Pages

  • Re: URGENT - plz help - Duplicate Keys in Hashmap :(
    ... I know that ideally we need to override equals() and hashcode() but ... do I need to do that for simple "String" Keys as well? ...
    (comp.lang.java.programmer)
  • Re: why in class Boolean, hashcode() of "true" is 1231 and of "false" is 1237?
    ... The Integer hashCode() is the primitive int the Integer represents. ... That will have a pretty poor distribution of hash values, because the integer values that arise in actual running code don't tend to be completely randomly distributed. ... keys' hashCodes. ...
    (comp.lang.java.programmer)
  • Re: Generic Collection with reference types
    ... Do any of you have the time/desire to explain what GetHashCode is used ... Is there anything out there that really explains the guts of ... Two keys which are equal should *definitely* have the same hashcode, ...
    (microsoft.public.dotnet.languages.csharp)
  • how does hashCode work?
    ... java implements hashCode? ... I want to be able to take two keys and make sure that they are placed ... I figure I could just write my own hashCode and equals, ... I'd rather manipulate the standard way. ...
    (comp.lang.java.help)
  • Re: Arrays as key in a HashMap
    ... You can't use arrays literally as keys. ... hashCode for them. ... Absolute identity is probably not what you want. ...
    (comp.lang.java.programmer)