C
chris brat
Hi
I'm doing something with Sets and Maps that I think is valid code but
I'd like an outside opinion on whether or not I am creating a memory
leak with the temporary HashMap that I am using.
My uncertainty is with the following lines in my example (the last
three of the method) :
result = new HashSet();
result.addAll(myMap.values());
return result;
My example :
~~~~~~~~~~
I have a method that does quite a bit of processing and I use a map to
do lookups for already processed data. The method must return a Set
instance.
private static final Set getSomeDataAndUpdate(String [] mapIds){
Set result = null;
// map that is used to simplify lookups
Map<String, String> myMap = new HashMap();
...
Some JDBC code which will populate entries in the the map
with a database key (as the map key)
and a string (as the map value for the key)
...
// by this stage the map is populated - process the
// supplied String array parameter
// and update the value in the map if the key exists
for (String mapId : mapIds){
if (myMap.containsKey(mapId)){
// update the data - the text is not important
myMap.put(mapId,"found you");
}
}
result = new HashSet(); // the result I want
result.addAll(myMap.values()); // dont want the map keys
return result;
}
Should I be emptying the HashMap and then setting it to null or will
the garbage collector handle this?
Thanks
Chris
I'm doing something with Sets and Maps that I think is valid code but
I'd like an outside opinion on whether or not I am creating a memory
leak with the temporary HashMap that I am using.
My uncertainty is with the following lines in my example (the last
three of the method) :
result = new HashSet();
result.addAll(myMap.values());
return result;
My example :
~~~~~~~~~~
I have a method that does quite a bit of processing and I use a map to
do lookups for already processed data. The method must return a Set
instance.
private static final Set getSomeDataAndUpdate(String [] mapIds){
Set result = null;
// map that is used to simplify lookups
Map<String, String> myMap = new HashMap();
...
Some JDBC code which will populate entries in the the map
with a database key (as the map key)
and a string (as the map value for the key)
...
// by this stage the map is populated - process the
// supplied String array parameter
// and update the value in the map if the key exists
for (String mapId : mapIds){
if (myMap.containsKey(mapId)){
// update the data - the text is not important
myMap.put(mapId,"found you");
}
}
result = new HashSet(); // the result I want
result.addAll(myMap.values()); // dont want the map keys
return result;
}
Should I be emptying the HashMap and then setting it to null or will
the garbage collector handle this?
Thanks
Chris