Results 1 to 15 of 15
- 03-28-2011, 07:27 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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:
Problem I get is on lineJava 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)); } }...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?Java Code:this.findFish.get("Shark").add(new Fish("Shark","Carnavour", "Salt Water",20));
many thanks in advance
-
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.'
- 03-28-2011, 07:50 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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
-
cannot find symbol means the method doesnt existthe compiler says cannot find symbol - method add(Fish)
i don't doubt 'add' exists, but the compiler is telling you that you can't add(Fish fishobj)
- 03-28-2011, 08:36 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
-
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
- 03-28-2011, 08:42 PM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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 getLast edited by doWhile; 03-28-2011 at 08:44 PM.
- 03-28-2011, 09:12 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
-
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
- 03-28-2011, 09:32 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
-
can you copy the compiler error exactly?
- 03-28-2011, 09:43 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
- 03-28-2011, 10:06 PM #13
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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
- 03-28-2011, 10:34 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
- 03-29-2011, 08:25 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
Hello again folks
Thanks for the help last night. I think I've finally worked it out. Here's how it looks now:
Now I have another question, I'm having trouble with this line: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)); } }
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?Java Code:this.findFish.get(eachFish).printString();
Thanks
Similar Threads
-
Populate select list with Java/Ajax
By Jeremy720 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-20-2011, 03:14 PM -
Populate an array dynamically
By mmarkym in forum New To JavaReplies: 11Last Post: 12-31-2010, 03:31 PM -
Ripping apart a array list to populate a vector
By Adrien in forum AWT / SwingReplies: 0Last Post: 03-07-2010, 09:55 PM -
Help Pls!! Jcombobox populate with mysql
By kwink in forum AWT / SwingReplies: 1Last Post: 03-23-2009, 04:11 AM -
how to populate data in drop-down box
By ma-la in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-01-2008, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks