3 Replies Latest reply: Dec 18, 2006 8:08 PM by 807607 RSS

    List<String>

    807607
      I want to build a HashMap<String, List<String> and this is the method I am using to add keys and values to the Map.

      I do not know what I am doing wrong:

      public void buildMap(String key, String value){
                List<String> tempList;
                
                if(map.containsKey(key)){
                     tempList = map.get(key);
                     tempList.add(value);
                     map.put(key, tempList);
                }else{
                     tempList = new List<String> //here is where I get the error: List.add cannon be resolved to a type
                     tempList.add(value);
                     
                     map.put(key, tempList);
                }
      }

      Message was edited by:
      douglas18
        • 1. Re: List<String>
          796254
          right, List is an interface. if you call new on it, you have to provide the implementation right there.

          you might choose a concrete implementation of that interface (e.g., ArrayList or LinkedList). The choice depends on how you'll use the value.

          %
          • 2. Re: List<String>
            796254
            The method might look like this:
            public void buildMap(String key, String value){
            List<String> tempList;
            
            if(map.containsKey(key)){
            tempList = map.get(key);
            tempList.add(value);
            map.put(key, tempList);
            }else{
            tempList = new ArrayList<String>();
            tempList.add(value);
            map.put(key, tempList);
            }
            }
            %
            • 3. Re: List<String>
              807607
              ok, thanks I will try this.

              Message was edited by:
              douglas18