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,
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
First of all the title of the question is incorrect.
You cannot have duplicate keys in a map!

Essentially, you need to structure the values in the map as a List. See below:

 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapExample {

    private static final Map<String, List<Integer>> map = new HashMap<>();

    public static void main(final String[] args) {
	addToMap("Monday", 123);
	addToMap("Monday", 789);
	addToMap("Tuesday", 9963);
	addToMap("Wednesday", 759);
	addToMap("Monday", 852);
	addToMap("Tuseday", 546);
	addToMap("Wednesday", 859);

	System.out.println(map);
    }

    public static void addToMap(final String key, final Integer value) {
	List<Integer> values = map.get(key);
	if(values == null){
	    values = new ArrayList<>();
	}
	values.add(value);
	map.put(key, values);
    }

}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
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.
srikoteshAuthor Commented:
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..
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

CEHJCommented:
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.
srikoteshAuthor Commented:
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
CEHJCommented:
and i have to pass these ids to some other table to get the result.
That's woolly. Please expand
srikoteshAuthor Commented:
after passing these ids to the third table
I will get the average of ids create on each day.
CEHJCommented:
How does one 'pass ids' to a table - what does that mean?
srikoteshAuthor Commented:
sorry iam executing query by passing these ids
CEHJCommented:
OK, so you feed in the list of ids into the query then
gurpsbassiCommented:
Can you not do this on the database layer instead?
gurpsbassiCommented:
@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.
gurpsbassiCommented:
@srikotesh. You have lost me on what you are trying to acheive.

Is the original question answered?
srikoteshAuthor Commented:
yes I am able to achieve my result
srikoteshAuthor Commented:
yes I am able to achieve my result
srikoteshAuthor Commented:
Excellent
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.