• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding different objects to same in HashMap?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.... learning is good thing.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright.... I'll take it.

Janeice
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interviewer might be expecting this
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Priyank Nautiyal wrote:Hi Guyz, . . . rite

Please use the correct spelling of words, for reasons explained here.
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic