I have HashMap HashMap<Integer, List<String>> storeR
and it stores "22,name1,name2"
..but is there any way to remove name1
from the 22
? i just want to remove only name 1 from 22. How do i do that.?I hope you can help me with it. Thank you.
Get the list out of your map and remove the element you want to have removed:
storeR.get(22).remove("name1");
If there is no key 22
in the map, or it's value is null
this will throw a NullPointerException
, though.
Given the questions you've asked around this, you'd be better off using a third party library: Guava has a Multimap
interface with various implementations - you'd probably want ArrayListMultimap
.
You can then just write:
multimap.remove(22, "name1");
If you really want to keep doing all the work manually yourself, you can call get
on the map to return the list, and then remove
on the list to remove the value. But you should consider whether you want to also remove the key entirely if you've removed the last value - and of course if you don't know whether the key already exists in the map, you need to only conditionally call remove
...
ArrayListMultimap in Guava is designed for the case.
See: http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.