Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

How to store and get the duplicate keys and values in map

Hi Experts,

Day_Name    ID
===============
Monday      123
Monday      789
Tuesday     9663
Wednesday   759
Monday      852
Tuseday     546
Wednesday   859


how to store these values in map
i want to store Monday name along with ids in map
how to get these values from
is it possible to get the values from map
because i have tried below example to get the values

Map<String, Integer> map = new HashMap<>();				
		map.put("Monday", 1245);
		map.put("Monday", 3456);
		map.put("Tuesday",4578);
		System.out.println(map.get("Monday")); 

Open in new window

           
I got the o/p as one value:
3456

can some one suggest me how to get all Monday ids
Tuesday ids ,wednesday ids.

If not possible with map tell me the alternative solution to get the values.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
A Map is not merely a collection of name value pairs (which is what you want)
You should make a collection of a class encapsulating the data you wish to model (a day name and id) and then fill it.

A 'multimap' (which is what gurpsbassi is recommending) is a possibility but is weaker from an OO design perspective. For instance, using string literals is asking for trouble - you'd only need to make minor typos such as the ones below to get some nasty bugs.
addToMap("Tuseday", 546);

Open in new window

Tuseday     546

The strongest way to model the day is with an enum. Of course you would still need to back the collection in my suggestion with a Map.
Avatar of srikotesh
srikotesh

ASKER

Hi Experts,

From the above code i am able to insert in map successfully.
now i have another  query which will give same kind of output.

Day_Name    ID
===============
Monday      1001
Monday      1002
Tuesday     1003
Wednesday   1004
Monday      1005
Tuseday     1006
Wednesday   1007

the above output also i want to store in map

when i call map.get("Monday");
will i get the both results of monday ids?
Monday      1001
Monday      1002
Monday      1005
Monday      123
Monday      789
suggest me how to do..
will i get the both results of monday ids?
What do you mean 'both results'?

You're not thinking about what's happening. You look up the List of values with a key ("Monday") and you get those values. What you do with them is up to you. If you want them printed like that, then that's just a matter of formatting the output. You're not actually stating your GOAL here, so it's hard to advise.
I have two different tables

one is network table and another one is system table
from these two tables i need to get ids which are created one week ago.
when i execute the first query i will get the last 1 week ids.
LIKE
Monday  1234
when i execute the second query i will get the similar kind of result but id will differ day name will be same.

so i need to collect all monday ids  from the both the table and i have to pass these ids to some other table to get the result.

suggest me how to do
and i have to pass these ids to some other table to get the result.
That's woolly. Please expand
after passing these ids to the third table
I will get the average of ids create on each day.
How does one 'pass ids' to a table - what does that mean?
sorry iam executing query by passing these ids
OK, so you feed in the list of ids into the query then
Can you not do this on the database layer instead?
@CEHJ I agree with your comments about modeling the days etc as domain objects and not just strings and lists etc. However I think the author of the question is a long way from appreciating this.
@srikotesh. You have lost me on what you are trying to acheive.

Is the original question answered?
yes I am able to achieve my result
yes I am able to achieve my result
Excellent