0

I created a hashmap as shown below:

Map<String, String> streetno = new HashMap<String, String>();

streetno.put("3", "Sachin");
streetno.put("2", "Dravid");
streetno.put("1", "Sehwag");
streetno.put("5", "Laxman");
streetno.put("4", "Kohli");

Now I want to create a new hashmap where key of the above hashmap becomes value and value becomes key as shown below:

Map<String, String> streetname = new HashMap<String, String>();

streetname.put("Sachin", "3");
streetname.put("Dravid", "2");
streetname.put("Sehwag", "1");
streetname.put("Laxman", "5");
streetname.put("Kohli", "4");

I don't know how to do that.. Can anyone help me out with this..

1
  • 1
    Note that guava, the google collections api, supports the notion of a BiMap that supports an inverse() operation to do just this. Usefully it does this without making a copy of the data. See here for api details Commented Feb 26, 2012 at 14:33

3 Answers 3

4
Map<String, String> streetname = new HashMap<String, String>();

for (Entry<String,String> e : streetno.entrySet()) {
  streetname.put(e.getValue(), e.getKey());
}

Here, the for loop iterates over all entries (i.e. key/value pairs) in the original map, and inserts them into the second map with the key and value swapped over.

It is probably a good idea to check that put() returns null. If you get a non-null value, this means that the values in streetno are not unique. Since this is homework, I leave it to you to figure out the consequences, and how best to handle this.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah I don't like it when somebody just solves the homework. Help a bit, but do not do all the work. After all it is homework so that the student will put some effort in it.
I hope your original values are unique; otherwise you're loosing data
2

Perfect you are almost there. Now you need to iterate the first hash map keys and simulate what you have done in those 5 lines:

streetname.put("Sachin", "3");
streetname.put("Dravid", "2");
streetname.put("Sehwag", "1");
streetname.put("Laxman", "5");
streetname.put("Kohli", "4");

Tip: iteration over map might be a bit tricky for you, but usually it is done like that:

for (String key : streetno.keySet()) {
...
}

Good luck with your homework!

2 Comments

I didn't downvote you but... perhaps the downvoter took offence to streetno.keySet() you should really be using streetno.entrySet() otherwise you're going to have to access the map again to get the value.
This is true, probably it will be a bit slower, but I think this will be good enough for homework. I Wanted to avoid the entrySet, because I was afraid I will confuse the student (I don't know his level of expertise). Even with my solution the complexity stays O(n).
0

Java 8:

Map<String, String> streetname = 
    streetno.entrySet()
            .stream()
            .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));

Note:

If you are tempted to use parellelstream() instead of stream() think twice about it. This would only be appropriate if your Map is extremely large.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.