Copying into ArrayList

I

Ike

I have a HashMap which has Strings as Keys, and stores ArrayList Objects. I
have a method then that loooks into the HashMap, and, if the Key for the
word is found, returns the ArrayList, and if not, creates a new Arraylist
and both adds it to the HashMap as well as returns the ArrayList.

However. in the step where if the key is found in the HashMap, its is not
copying the ArrayList object stored in the HashMap to ArrayList I want to
return. Can anyone tell me why? Code is given below:

private ArrayList getElemementAncestorsListNoSemantics(String s){
ArrayList retlist=new ArrayList();
if(hmap3.containsKey(s)){
retlist.addAll((ArrayList)hmap3.get(s));// <---PROBLEM *
}else{
//get the arraylist for the String s, and put it in both the
retlist to return and the HashMap hmap3
//for later lookup
retlist.addAll(makemynewarraylist(s));
hmap3.put(s,retlist);
}
return retlist;
}

* I am certain that the ArrayList for the given key, s, exists at this
point. It is just not being copied over into the ArrayList retlist.

//-Ike
 
T

Thomas Hawtin

Ike said:
private ArrayList getElemementAncestorsListNoSemantics(String s){
ArrayList retlist=new ArrayList();
if(hmap3.containsKey(s)){
retlist.addAll((ArrayList)hmap3.get(s));// <---PROBLEM *

Have you tried a debugger or System.err.println to show what
hmap3.get(s) is returning?
}else{
//get the arraylist for the String s, and put it in both the
retlist to return and the HashMap hmap3
//for later lookup
retlist.addAll(makemynewarraylist(s));
hmap3.put(s,retlist);

Here you are returning a list that becomes a value of the map. Above you
are explicitly copying the list. Presumably one of these is wrong.
}
return retlist;
}

Tom Hawtin
 
R

Ralf Seitner

Ike said:
I have a HashMap which has Strings as Keys, and stores ArrayList Objects. I
have a method then that loooks into the HashMap, and, if the Key for the
word is found, returns the ArrayList, and if not, creates a new Arraylist
and both adds it to the HashMap as well as returns the ArrayList.

However. in the step where if the key is found in the HashMap, its is not
copying the ArrayList object stored in the HashMap to ArrayList I want to
return. Can anyone tell me why? Code is given below:

private ArrayList getElemementAncestorsListNoSemantics(String s){
ArrayList retlist=new ArrayList();
if(hmap3.containsKey(s)){
retlist.addAll((ArrayList)hmap3.get(s));// <---PROBLEM *
Hi!
Perhaps you want to return the reference of the ArrayList by doing
retlist = (ArrayList)hmap3.get(s);
}else{
//get the arraylist for the String s, and put it in both the
retlist to return and the HashMap hmap3
//for later lookup
retlist.addAll(makemynewarraylist(s));
and retlist = makeMyNewArrayList();
Depends on what you want to do...
hmap3.put(s,retlist);
}
return retlist;
}
Perhaps your ArrayList simply doesnt contain any elements and thats your
problem.
Think you have to debug a little bit more...
* I am certain that the ArrayList for the given key, s, exists at this
point. It is just not being copied over into the ArrayList retlist.
Of course the key s is in your HashMap at that point, you are checking
for it by containsKey(s)...
bye, Ralf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top