• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

HashMap logic

 
Ranch Hand
Posts: 644
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76432
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 644
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 644
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 644
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was pretty simple. Here is the sample program.
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:




 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic