This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Adding different objects to same in HashMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding different objects to same in HashMap?" Watch "Adding different objects to same in HashMap?" New topic
Author

Adding different objects to same in HashMap?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all

Is it possible to add different values or objects to the same key of hashmap other than collection or array


Creativity is nothing but Breaking Rules
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

You can change the value for the keys, but each key maps to "at most one" value (see the Map API below).

HashMap is a class that implements Map.

What is it you're trying to do? There might be a different way....

Janeice


When you do things right, people won't be sure you've done anything at all.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
It is not possible (you indicated that it is possible through collections, So I assume, you know that way of adding). If it was possible, how could you have retrieved a value, given one key. So, values need to be in some sort of collection, on which you can iterate or whatever, which means that for the top hashmap, there is one value at any time. What is the type of that value, that is your concern.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
No actually

I had gone for interview in Iflex the interviewer asked me the question
How to add more values to same key..?
i told him it is possible by collection or arrays and i also told him if we add directly it will overide the previous values and finally i didn't agree with my answer
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
I also faced once this question. Probably What they mean is if you add one more value to same key, how you will make sure it is not overridden. So, even if you put say list mapped to key. Now question is you add one more value, rather than replacing the list, how you will add into the list? (that is still a guess)
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Now question is you add one more value, rather than replacing the list, how you will add into the list?


Thats correct but what we can conclude finally for this question
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Here's another thread about this problem....

One of the posters mentioned a comma separated String as a value type... maybe that's what the interviewer was talking about? That's not TECHNICALLY a collection..... and you could get the information out by iterating and adding to an array......

**shrug**
Janeice
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Simple, override put method of hashmap.

one way is
if you are sure there is going to be more than one value. first time you create a collection and put your value, next time you check for existence of collection, if it is there, just add your next value in this collection.

second way is
add for first time, normal. Next time, if you see there is already a value, create object of collection and put first value along with second value in it. next time onwards, you simply add in this collection. BUT this is very bad design as it is not generic and hence not typesafe.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Rahul --

I think both those methods use collections, which was what the question was asking to avoid....

If you could use collections, you could use an array or a list and be done with it. I just did a project where the value had to be changed (incremented).... the key here is that we need a map with multiple values for each key without using a collection or array.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Janeice DelVecchio wrote:the key here is that we need a map with multiple values for each key without using a collection or array.


Do you think we can achieve that feat without using collections/arrays. I don't and if this is true, neither interviewer can. So, probably best way to clarify the question with interviewer, what exactly he is looking for? Moreover, I am aware what the question is? See my firs post and then second post of interpretation of the question from different angle. Sometimes, that is what they do.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
See my firs post and then second post of interpretation of the question from different angle. Sometimes, that is what they do.


Even i also agree with you and finally i think it's not possible to add more values to same key with out using collection and array finally if its possible let me know
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

If the question the OP had was not specifically asking for "without the use of collections or arrays" I would agree with you. And before I looked at that other thread I might have agreed that there's no way to do it...

I think a String (or StringBuilder) with comma separated values is what the answer is. Of course, using a collection or array would be easiest, but if that's not part of the spec for the problem. With a StringBuilder you could iterate through it and put the substrings into arrays to get the information out. Or, depending on the way the information is used... you might not even have to do that. It might go right to an application that reads CSVs...

I'd like to see what some of the other ranchers think....
Janeice



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
The interviewer was not looking for a "right" answer, but for some sort of understanding of the problem. Did nobody say that a Map<K, V> represents a function? Did nobody know what a function is?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Campbell Ritchie wrote:The interviewer was not looking for a "right" answer, but for some sort of understanding of the problem.


I just thought interviewers were looking for people who didn't say things couldn't be done.

Did nobody say that a Map<K, V> represents a function? Did nobody know what a function is?


Uhmmm..... if we're talking about functions in math, I know what they are, but I don't necessarily think Map is a function in that sense. I guess in some cases, but V is not always a function of K..... and as I'm chirping on about this I am thinking less and less that this is what you were referring to.......


Good thing it wasn't my interview....
Janeice
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
I don't know Campbell intention to write that statement. But if it was to confuse, then it was nice attempt.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I'm pretty sure his intention was to show us what we don't know so we could discuss it. Then later he'll come by and tell us we were wrong the right answer.

You can't learn unless you start with learning what it is that needs to be learned.
Janeice
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Hmm.... learning is good thing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
No intention to confuse.


Read this: (from the the Java Tutorials):
The Map Interface

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.
A function to a mathematician is something which takes an input, and returns an output; the same input always returns the same output.

You "put" keys and values into a Map; the "key" is the input, and the value is the output. If you set up a Map it is a function; the same input (key) always returns the same output (value). If you put a different value, then you are altering the function.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

This is great and interesting, but I'm having a hard time putting it all together......

What does this have to do with the interview question?

Janeice
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Campbell, though your original statement has really confused me that whether you are saying an interviewer may ask such type of question or you want an upper hand? The explanation was indeed good.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Janeice DelVecchio wrote:This is great and interesting, but I'm having a hard time putting it all together......

What does this have to do with the interview question?

Janeice

Campbell means that a Map can never hold multiple values - it's like a function that returns multiple values for the same input. In regular math that's impossible.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Campbell Ritchie wrote:Did nobody say that a Map<K, V> represents a function? Did nobody know what a function is?


Well, indeed, to a mathematician a function is simply a set of ordered pairs with the restriction that if the set includes (a, b) and (a, c) then b = c.

Officially, that's what it is anyway. But there aren't many mathematicians who really think of a function that way, and the one I knew who did think of them that way went on to do his Ph.D. on the topic of infinite-dimensional vector spaces.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Interview questions, as I said earlier, may not have a "right" answer. The idea behind asking such a question, which has actually got no right answer, is to see how you think around the problem. If you come out with a very quick answer, you will be regarded as not answering the question.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Janeice DelVecchio wrote:This is great and interesting, but I'm having a hard time putting it all together......
But next time you get questions about Maps, maybe you will remember that a Map represents a function, and will look up a function and see what a function is . . .
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I was just thinking that I suppose if there are just VALUES in with the keys (<Integer, Integer> or any combonation of int, double, float, long), then it's a no brainer. It's a function. One y value for every x. You can graph it. But if you map to OBJECTS...

I mean, how do you make a function out of <String, String> or <Dog, Owner>?

That's where the hangup is. You don't see a lot of graphs in Calculus graphing which dog goes to which owner.....


Janeice
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Theoretically speaking, a function is just an operation f that takes input x and produces output y: f(x) = y. Intuitively, x and y are numbers, but they actually don't have to be.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

It doesn't really matter what the keys and values represent. What matters is the relationship: that there is at most one value for each key.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Alright.... I'll take it.

Janeice
Priyank Nautiyal
Greenhorn

Joined: Oct 18, 2005
Posts: 16
Hi Guyz,

Read the specification of the Map interface.

"An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. "
This means that you cannot have duplicate values for a key.

In case you do manage to do that it would not be a Map, rite


Cheers,<br />Priyank<br />SCJP-96%,SCWCD-85%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

interviewer might be expecting this
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
santhosh.R gowda wrote:
Is it possible to add different values or objects to the same key of hashmap other than collection or array


A somewhat intrusive solution would be to add a "next" field to the objects. Objects associated with the same key could then chain themselves together in a linked list.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Priyank Nautiyal wrote:Hi Guyz, . . . rite
Please use the correct spelling of words, for reasons explained here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding different objects to same in HashMap?