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
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......
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.
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.
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.
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
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....
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....
Read this: (from the the Java Tutorials):
A function to a mathematician is something which takes an input, and returns an output; the same input always returns the same output.
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.
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 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?
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.
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.
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.....
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
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.
Please use the correct spelling of words, for reasons explained here.
Priyank Nautiyal wrote:Hi Guyz, . . . rite