> I have developed a parsing program to handle large csv files and
> compute sums. The program was
[quoted text clipped - 50 lines]
> Any help as to why StringBuffer produces bad sums is greatly
> appreciated.
Hello,
Replacing Strings with StringBuffers is only a good idea when you are
incrementally constructing text. As soon as you see a pattern of
stringVar += anyVar;
repeating multiple times you should start thinking abouty using
StringBuffers. In any other situation you should stick to Strings. This
includes when you want the result of many += operations as a key: use the
toString() value as the key, not the StringBuffer.
Using a mutable object as a key in a Set/Map is dangerous since the key
could be modified after using it as a key. This WILL compromise the
containers integrity.
Regards,
Silvio Bierman
shriop - 15 Mar 2005 20:25 GMT
I'm not sure that what silvio was saying is clear enough to answer your
simple question. You're using an object type as a key. The only way to
retrieve the value is to use the same object. When you were using
strings as a key, they're basically considered a value type. You can
then retrieve them using any other string that is equal. This is the
same for adding in duplicates that are supposed to overwrite the
original. That is why silvio just simply said to use the .toString()
return value as the key.