• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete key from value in maps  RSS feed

 
Antonis Kaklis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!
Well I' facing the following problem
I have two maps(ConcurrentHashMaps),and i want to delete a key based on a specific value from the first map,
and with the same key, I want to delete the equal entry, if there is one, on the other map.
I try the following code but I think is wrong. Please help:


Any other suggestions?
Thanks in advance
Antonis
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think the code is wrong? What happens if you run it, and how does that deviate from what you think it should do?
 
Antonis Kaklis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It deletes the entry from the first filemap,
but not the corresponding entry from the second filemap.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cast to PSXResourceInfo in line 5 is unnecessary; because of the generics, the compiler already knows that the value is a PSXResourceInfo object.

The if-statement in line 9, where you check if the value is in the map, looks unnecessary. You're looping through that same map, so the value you are checking will always be in the map.

The loop in lines 14-18 looks unnecessarily complex. You don't need to loop at all to delete an entry from renamemap, if you already know the key that the entry is under in renamemap. Just replace lines 14-18 by: renamemap.remove(keyToDel);

The reason why it doesn't work is maybe because renamemap contains something different than what you expect. Try adding some System.out.println() statement to see what's happening exactly or step through the program using a debugger.
 
Antonis Kaklis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much
 
Antonis Kaklis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well something happened again.
if i add to the following code:

the lines
System.out.println(filemap.size());
System.out.pritnln(renamemap.size());

i found out that the first fielmap is empty after a specific time interval which is what i want,
but the second renamemap remains the same!!! Eventhough that it has the same Key-Values with the first map
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antonis Kaklis wrote:i found out that the first fielmap is empty after a specific time interval which is what i want,
but the second renamemap remains the same!!! Eventhough that it has the same Key-Values with the first map

You haven't shown us anything about the definition of the renamemap variable, but I think if you look, you will find it doesn't have the same keys as the filemap Map. If it did (and if it's a normal Map) then your code would work.
 
Antonis Kaklis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you were right, the keys were different.
Thanks

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!