1. is it possible to put 2 values with 1 key into a HashMap? I know the regular put(k,v) won't do it..but I'm trying to override many functions to fit my needs and I can't figure out a way to do it.. let's say the key is a name and the value is a phone number..so I should have a bucket with few values in the case that there are few people with the same name.
i.e - John Smith-> 87523579,34537453
if that's not possible what other structure can I use to make that work?

2. if I have a BIG amount of different keys that I want to put in my HashMap, is there a way to make the map class more efficiant when it comes to functions like get,put,remove, etc?

thank you.

Recommended Answers

All 4 Replies

1) Yes, it's possible. What'll happen is the first value will be overwritten, though, which isn't what you want. I'd look again at your structure. If person to phone number is a one to many relationship, perhaps you want to map names to arraylists of phone numbers. If there are multiple Persons with the same name, you want separate Person objects, retrievable by various keys (including name), and each of those Persons has an arraylist of phone numbers. Phone number is an object: minimally, it's two strings, one containing the number (not an int, please, since you're not doing math with them) and one containing some identifying tag, like "home" or "office" or "mobile" or "mistress's flat")

Okay, now you have a collection of Persons. You want them searchable by name? Your map is a map of <String, ArrayList<Person>>. persons.get("John Smith") returns the bucket of John Smiths - if there's more than one, you have to choose. Since you have the Person in hand, you can get his phone numbers.

That's one way to do it, at least.

2) If you're mapping custom objects, you'll want to look into hashcodes. There's a lot of talk out there about good hash codes, so there's a lot of reading to do. Have fun.

2) If you're mapping custom objects, you'll want to look into hashcodes. There's a lot of talk out there about good hash codes, so there's a lot of reading to do. Have fun.

Not to forget the equals method, of course. ;-)

Figured he'd come across that in his research... :)

thanks guys ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.