Does the Map#get method return the value that the key is mapped to or does it return a reference to the value?
Code(This is a program I am working on):
Map<String,Vector<String>> map=new TreeMap<String,Vector<String> >();
for(String line:services)
{
String[] set=line.split(" ");
Vector<String> t=new Vector<String>();
String BioService=set[0];
int i=1;
while(i<set.length)
{
t.clear();
if(map.containsKey(set[i]))
t=map.get(set[i]);
t.addElement(BioService);
map.put(set[i],t);
/*if i put t.clear() here i get null values in my output*/
i++;
}
}
It returns a reference to the Object.
You will never have the actual Object in Java, only ever a reference to it.
You do have actual primitives (and cannot have references to primitives, only references to Objects that wrap primitives or something like that), but of course primitives cannot be put into maps, only Objects can.
Let’s say you have the following code:
You only ever use one single vector. You put your vector into the map, and then on the next iteration you clear it. Well, you just cleared the vector that’s inside the map! Just because you put it in the map doesn’t mean it can’t be changed.
Your best bet, I feel, would be to make a new vector for each entry.
So replace
with