• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding different objects to same in HashMap?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

Is it possible to add different values or objects to the same key of hashmap other than collection or array
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49372
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.... learning is good thing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49372
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49372
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49372
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright.... I'll take it.

Janeice
 
Priyank Nautiyal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interviewer might be expecting this
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49372
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyank Nautiyal wrote:Hi Guyz, . . . rite
Please use the correct spelling of words, for reasons explained here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic