• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashmaps (again)  RSS feed

 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evening folks!

I could probably work around this but out of curiosity...

If I have HashMap1.keySet and HashMap2.keySet, how would I compare the 2 sets and return only those key/value pairings in Map2 but NOT in Map1?

Many thanks in advance for any help!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ward wrote:return only those key/value pairings in Map2 but NOT in Map1?

Does that mean that both the key and value need to be the same for an entry to be considered to be in both Maps or is it just the key that is important ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd try looking at the Set.removeAll method.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Does that mean that both the key and value need to be the same for an entry to be considered to be in both Maps or is it just the key that is important ?


Edit* Sorry had a brain fart, the keys are days + times, values are appointments. If the day + time is already "busy" then that's it. Basically trying to find "free" days + times.

I'll go have a read about the removeAll method, thanks!
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred! Exactly what I needed!

So in laymans, Set.removeAll(), takes one Map and removes all keys from it that occur in Map2?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ward wrote:So in laymans, Set.removeAll(), takes one Map and removes all keys from it that occur in Map2?


No...

Set.removeAll() takes one Set, not one Map, and removes all elements that occur in the other - be they 'keys', 'widgets', 'fubars', or anything else.

I am NOT certain what happens if you try and remove an element that is not there - I would assume it would be ok, but I didn't really scour the API that closely.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry! My mistake, I meant map.set.

Thanks again.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I am NOT certain what happens if you try and remove an element that is not there - I would assume it would be ok, but I didn't really scour the API that closely.

The remove method will return false and do nothing.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!