I want to create a map that has two key :
map.put (key1,key2,value1);// Insert into map
map.get(key1,key2); // return value1
i have looking into multikeyMap but i don’t know how i will do it
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sounds like you just want a key which is created from two values. You may well find that those two values should naturally be encapsulated into another type anyway – or you could create a
Key2<K1, K2>type. (The naming here would allow forKey3,Key4etc. I wouldn’t encourage you to go too far though.)For something in between, you could create a private static class within the class where this is really needed (if it’s only an internal implementation detail). If it’s not a natural encapsulation (e.g. it’s something like “name and population”, which doesn’t make sense outside this specific scenario) then it would be good in terms of keeping meaningful property names, but without exposing it publicly.
In any of these scenarios, you’ll end up with a new type with two final variables which are initialized in the constructor, and which contribute to both
equalsandhashCode. For example:The more situation-specific types would be slightly simpler as they wouldn’t be generic – in particular this would mean you wouldn’t need to worry about the type arguments, and you could give the variables better names.