0

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.

1
  • 5
    What have you tried? Have you tried looking into documentation of HashMap and List? Commented Feb 16, 2013 at 15:53

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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...

Comments

0

ArrayListMultimap in Guava is designed for the case.

See: http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.