Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap logic

 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two hashmaps.

HasMap1 has keys 100 , 200
HashMap2 has keys 200,300,400,100

I need to remove the elements with keys 300 and 400 which are also in HashMap1 from HashMap2. How to achieve that? I thought of different ways but the problem is even if I separate the duplicates keys how to make sure I remove only those from HashMap2.


thanks,
Trupti
 
Campbell Ritchie
Sheriff
Pie
Posts: 49440
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Draw a diagram on a piece of paper and the logic will become obvious.
You need to be specific. By “also in HashMap1” do you mean they have the same “V”s?
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti,

I think you should check out once the API for Hashmap here.

For reading keys of the map, use keySet(). Iterate through them and proceed like for instance:

if(hashMap1.get(key).equals(hashMap2.get(key)){
hashMap2.remove(key);
}

Thanks and Regards,
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Draw a diagram on a piece of paper and the logic will become obvious.
You need to be specific. By “also in HashMap1” do you mean they have the same “V”s?


Yes the values are same.
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwini Kashyap wrote:Hi Trupti,

I think you should check out once the API for Hashmap here.

For reading keys of the map, use keySet(). Iterate through them and proceed like for instance:

if(hashMap1.get(key).equals(hashMap2.get(key)){
hashMap2.remove(key);
}

Thanks and Regards,
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com


I don't want to remove the duplicate key elements but the non dupe one.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively instead of iterating, you can use the retainAll() method in Set interface. First get the key set of the second map and call retainAll() on it with the first map's keys. Check methods - keySet() and retainAll()
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet its simple. Make use of !equals() i.e. not equals method.

Thanks and Regards,
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was pretty simple. Here is the sample program.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer something more simple than that. Based on John Jai's comment, I would replace this:



by this:




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic