0

as my question says I would like to insert my arraylist items into a hashmap.

This is my HashMap.

HashMap<String, Integer> unSorted = new HashMap<String, Integer>();

This is my ArrayList.

arrayList.add("1");

arrayList.add("2");

Adding into Hashmap.

unSorted.put(arrayList,50);

I am pretty sure I can't add an arraylist like this. I forgot I've got to iterate the arrayList and insert the values one by one.

How am I going wrong ?

Thank you for your time !

5
  • 4
    Haven't you got the answer yourself? :) "I forgot I've got to iterate the arrayList and insert the values one by one." Commented Nov 17, 2011 at 5:17
  • you want to put arrayList as key value or as value?? Commented Nov 17, 2011 at 5:21
  • Hi denis...my problem is dealing with my key type in the hashmap. Can I add an arrayList directly HashMap<String, Integer> unSorted = new HashMap<String, Integer>(); Commented Nov 17, 2011 at 5:21
  • 1
    Yes, you have your answer in your posted question. Iterate through the arrayList. something like this: unSorted.put(arrayList.get(0),0) ... Commented Nov 17, 2011 at 5:22
  • 1
    @Vinoth you may add hole arraylist directly but you need to change data-type in HashMap. Commented Nov 17, 2011 at 5:28

1 Answer 1

5

Iterate the ArrayList.

for(String item:arrayList)
 {
  unSorted.put(item,50);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.