Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding values to a list already against already existing key in HashMap

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,

I have a question. I declared a HashMap like:-

HashMap<String, ArrayList> map = new HashMap<String, ArrayList>();

ArrayList test = new ArrayList();
test.add("hardwork");
test.add("dedication");
map.put("sucess", test );

Now the data is like this in the Map
Key Value
sucess [harwork, dedication]

If i want to add values X and Y to the key "sucess", so that the value is like [harwork, dedication, X, Y]. How do i do it?

-Aditya
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get(key) is the way to extract a single value from a Map, which in this case will return an ArrayList. So you can do this:
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
success

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're not sure if the key is already present you can use the following technique:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic