2

I have to fetch the lis of data.

So i have used the arraylist of string with list.here how can i add the value on map.

I have used below code:

       static final String KEY_TITLE = "Category";
         static final String KEY_ARTICLE = "article";
          static final String KEY_IMAGE = "image";


 final ArrayList<HashMap<String,  List<String>>> songsList = new ArrayList<HashMap<String,  List<String>>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);
        NodeList nl1 = doc.getElementsByTagName(KEY_ARTICLE);
        NodeList nl2 = doc.getElementsByTagName(KEY_IMAGE);
        System.out.println(nl.getLength());
        for (int i = 0; i < nl.getLength(); i++) {
           //System.out.println(nl.getLength());
            HashMap<String, List<String>> map = new HashMap<String, List<String>>();
            map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));
           for (int j = 0; j < nl1.getLength(); j++) {
              map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title"));
              map.put( KEY_IMAGE,((Element)nl2.item(j)).getAttribute("url"));
             }
           songsList.add(map);
          }

Here am getting the below error on these (map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));) line:

The method put(String, List<String>) in the type HashMap<String,List<String>> is not applicable for the arguments (String, String)

How can i clear these error ..pls provide me solution for these ...

3 Answers 3

4

at this line

 map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));

you are adding a single string to instead of List

You have to create a List of String at that point and add your String to it and at the list to your map.

e.g.:

  List<String> myList= new ArrayList<String>();
  myList.add((Element)nl.item(i)).getAttribute("name"));
  map.put( KEY_TITLE,myList);

you have a loop. I think that i know what you want to achieve i'll update my code. This can help you avoiding overriding the map input

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

        for(int i = 0; i < nl.getLength(); i++) {
             String name = (Element) nl.item(i)).getAttribute("name");
             populateMap(map, name, KEY_TITLE);
            for(int j = 0; j < nl1.getLength(); j++) {
                String title=(Element) nl1.item(j)).getAttribute("title");
                populateMap(map, title, KEY_ARTICLE);

                String url=(Element) nl2.item(j)).getAttribute("url");
                populateMap(map, url, KEY_IMAGE);
            }
            songsList.add(map);
        }

Method populateMap()

void populateMap(HashMap<String, List<String>> map, String value, String key) {
        List<String> myList;
        if(!map.containsKey(key)) {
            myList = new ArrayList<String>();
            myList.add(value);
            map.put(key, myList);
        } else {
            myList = map.get(key);
            myList.add(value);
        }
    }
6
  • Hi.,i have following these answer:stackoverflow.com/questions/15737756/… here i have list of values ly.how can i add that list of values on map Apr 1, 2013 at 6:35
  • @user2218667 did you see my example? Apr 1, 2013 at 6:38
  • @user2218667 i see it. but there is a difference between what the done in that post and yours. HashMap<String, String> map = new HashMap<String, String>() but in yours HashMap<String, List<String>> map = new HashMap<String, List<String>>(); Apr 1, 2013 at 6:41
  • @user2218667 you have a loop. I think that i know what you want to achieve i'll update my code. This can help you avoiding overriding the map input Apr 1, 2013 at 6:47
  • ok yes exactly i want to override the map input.pls give me solution Apr 1, 2013 at 6:49
1

You get that error because a String is not a List<String>.

Either change your map declaration to HashMap<String, String> or enclose your String into a List<String> and give it to the method.

1

The key you're passing in is wrong. You're passing in a string, you need to pass in a list of strings. So create a list, stick the value you're trying to pass into the hash map into the list, then put that list into the hash map.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.