Results 1 to 15 of 15
  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default How to populate a List within a Map

    Hello folks, hope you can help me with a little task. I want to make a Map that populates with <String> List<Fish>, but am having issues. Here's the code:
    Java Code:
    import java.util.*;
    public class FishFarm
    {
       private HashMap findFish;
       public FishFarm()
       {
         Map<String, List<Fish>> findFish = new HashMap<String, List<Fish>>();
       }
       public void fish()
       {    
          HashMap findFish = new HashMap<String, List<Fish>>();      
       }  
       public void addFish()
       {
          List<Fish> temp;
          temp = new ArrayList<Fish>();
          temp.add(new Fish("Shark","Carnavour", "Salt Water",20));
          this.findFish.put("Shark", temp);
          this.findFish.get("Shark").add(new Fish("Shark","Carnavour", "Salt Water",20));
       }
    
    }
    Problem I get is on line
    Java Code:
          this.findFish.get("Shark").add(new Fish("Shark","Carnavour", "Salt Water",20));
    ...where the compiler says cannot find symbol - method add(Fish). So I can't get the List in the map to populate with the new object of type Fish. Any ideas?

    many thanks in advance

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    and do you have a method called 'add' which takes the parameter of type Fish? if you do i would suggest to call it another way other than starting with 'this.'

  3. #3
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Hi ozzyman, thanks for the reply. Not sure what you mean tough. add() is a method inhereted by the List interface. Can you please elaborate?

    Thanks

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    the compiler says cannot find symbol - method add(Fish)
    cannot find symbol means the method doesnt exist

    i don't doubt 'add' exists, but the compiler is telling you that you can't add(Fish fishobj)

  5. #5
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    cannot find symbol means the method doesnt exist

    i don't doubt 'add' exists, but the compiler is telling you that you can't add(Fish fishobj)
    Thanks again ozzyman. Unfortunately I still don't get what you mean. You can tell I'm a newbee at this:confused:

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    hummmmm i think you have the right idea but your syntax is wrong.

    when you get an object in an arraylist e.g.

    ArraylistExample.get(index) ---> this code returns a single item in the list, which means that if you try to use a List.add method, the object at the specified 'index' must also be a list, so that you can add an item to that list (becoming a 2 dimensional list). but i dont think thats what you mean to write

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    First, make sure you instantiate the instance variable - otherwise you will receive a NullPointerException.

    Second, your instance variable findFish HashMap does not use generics - in other words get returns an Object of type Object, which has no method add. Use generics or cast the value returned from get
    Last edited by doWhile; 03-28-2011 at 09:44 PM.

  8. #8
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    cannot find symbol means the method doesnt exist

    i don't doubt 'add' exists, but the compiler is telling you that you can't add(Fish fishobj)
    Quote Originally Posted by doWhile View Post
    First, make sure you instantiate the instance variable - otherwise you will receive a NullPointerException.

    Second, your instance variable findFish HashMap does not use generics - in other words get returns an Object of type Object, which has no method add. Use generics or cast the value returned from get
    Thanks for taking out the time to reply. Unfortunately I don't understand any of this. I'm now going to put my head in an airtight box.:eek:

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    doWhile told you to change this:

    HashMap findFish = new HashMap<String, List<Fish>>();


    to this:

    HashMap<String, List<Fish>> findFish = new HashMap<String, List<Fish>>();



    and that because you didn't do that, the Java Compiler assumed this:

    HashMap<Object> findFish = new HashMap...



    which means that, when you try this:
    .get("Shark").add

    what happens is this:

    Object.add // error

  10. #10
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    doWhile told you to change this:

    HashMap findFish = new HashMap<String, List<Fish>>();


    to this:

    HashMap<String, List<Fish>> findFish = new HashMap<String, List<Fish>>();



    and that because you didn't do that, the Java Compiler assumed this:

    HashMap<Object> findFish = new HashMap...



    which means that, when you try this:
    .get("Shark").add

    what happens is this:

    Object.add // error
    Thanks, but the error is the same, that box is looking more and more attractive as time goes on. Mabey I'm just no good at this stuff.:(

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    can you copy the compiler error exactly?

  12. #12
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    can you copy the compiler error exactly?
    cannot find symbol - method add(Fish)
    error on the following line:

    this.findFish.get("Shark").add(new Fish("Shark", "Salt Water",20));

  13. #13
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Suggested reading: Lesson: Generics (The Java™ Tutorials > Learning the Java Language)

    What to do should be pretty clear from the above, but here's a more detailed demonstration in semi-pseudo-code
    Java Code:
    /* no generics */
    Map map= new HashMap();
    map.put("test1", "test1");
    String s1 = map.get("test1");//compile time error - the map returns an Object
    String s2 = (String)map.get("test");//no error - you've cast Object to String
    Object s3 = map.get("test");//no error - no need to cast
    int length = map.get("test").length();//compile time error - Object has no length() method
    int length = ((String)map.get("test")).length();//OK - cast to String then call length
    .....
    /* Using Generics */
    Map<String,String> map= new HashMap<String,String>();
    map.put("test1", "test1");
    String s1 = map.get("test1");//error is now gone...no need to cast since the generics defined this map to hold String 
    int length = map.get("test1").length();//OK

  14. #14
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    Suggested reading: Lesson: Generics (The Java™ Tutorials > Learning the Java Language)

    What to do should be pretty clear from the above, but here's a more detailed demonstration in semi-pseudo-code
    Java Code:
    /* no generics */
    Map map= new HashMap();
    map.put("test1", "test1");
    String s1 = map.get("test1");//compile time error - the map returns an Object
    String s2 = (String)map.get("test");//no error - you've cast Object to String
    Object s3 = map.get("test");//no error - no need to cast
    int length = map.get("test").length();//compile time error - Object has no length() method
    int length = ((String)map.get("test")).length();//OK - cast to String then call length
    .....
    /* Using Generics */
    Map<String,String> map= new HashMap<String,String>();
    map.put("test1", "test1");
    String s1 = map.get("test1");//error is now gone...no need to cast since the generics defined this map to hold String 
    int length = map.get("test1").length();//OK
    :confused:

    Ok, now I'm really confused. I can't see how this helps me populate my map with my object? I know it's me and thanks for posting but I don't get it.

  15. #15
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default

    Hello again folks
    Thanks for the help last night. I think I've finally worked it out. Here's how it looks now:

    Java Code:
    import java.util.*;
    public class FishFarm
    {
       private Map<String, List<Fish>> findFish;
       public FishFarm()
    
       {
         //Map<String, List<Fish>> findFish = new HashMap<String, List<Fish>>();
        findFish = new HashMap<String, List<Fish>>();
       }
       public void fish()   
       {
               
          Set<String> key = findFish.keySet();
          for (String eachFish : key)
          {
             this.findFish.get(eachFish).printString(); 
           }
    
        }  
       public void addFish()
       {
          List<Fish> temp;
          temp = new ArrayList<Fish>();
          temp.add(new Fish("Shark", "Salt Water",20));
          this.findFish.put("Shark", temp);      
          this.findFish.get("Shark").add(new Fish("Shark", "Salt Water",20));
          temp = new ArrayList<Fish>();
          temp.add(new Fish("Perch", "Fresh Water",2));
          this.findFish.put("Perch", temp);      
          this.findFish.get("Perch").add(new Fish("Perch", "Fresh Water",2));
          temp = new ArrayList<Fish>();
          temp.add(new Fish("Shark", "Fresh Water",22));
          this.findFish.put("Shark", temp);      
          this.findFish.get("Shark").add(new Fish("Shark", "Fresh Water",22));
          
       }
    
    }
    Now I have another question, I'm having trouble with this line:
    Java Code:
    this.findFish.get(eachFish).printString();
    I know this isn't right. The method printString() belongs to a class called Fish. The fish objects are now in my ArrayList nested in the Map, so when I try to execute the code above I'm missing the part that allows me to drill one layer deeper to get to the content of the arrayList (i.e the Fish object). Any pointers on how I can achieve this?

    Thanks

Similar Threads

  1. Populate select list with Java/Ajax
    By Jeremy720 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-20-2011, 04:14 PM
  2. Populate an array dynamically
    By mmarkym in forum New To Java
    Replies: 11
    Last Post: 12-31-2010, 04:31 PM
  3. Ripping apart a array list to populate a vector
    By Adrien in forum AWT / Swing
    Replies: 0
    Last Post: 03-07-2010, 10:55 PM
  4. Help Pls!! Jcombobox populate with mysql
    By kwink in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 05:11 AM
  5. how to populate data in drop-down box
    By ma-la in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-01-2008, 12:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •