Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Sums and StringBuffers

Thread view: 
guyzdancin - 15 Mar 2005 17:21 GMT
I have developed a parsing program to handle large csv files and
compute sums.  The program was

developed and successfully tested using only String objects. I want
replace String objects with

StringBuffer.  The file is a record of electricity consumption
collected every fifteen minutes.

The total consumption must be summed.

The file contains duplicate lines and the first spec was to remove
duplicates.  This is

accomplished by adding each line as a String object to a HashSet.  So
this is where I started in

replacing String with StringBuffer.  The problem is that the
computations are wrong when the

StringBuffers replace the Strings in just this one line of code.

Here is the program in pseudo code:

    //eliminate duplicate lines
    //when lines were added as String objects
    //program produced accurate computations

    String nextLine = null;
    nextLine = getLine();
    while(nextLine != null){
        hashSet.add(new StringBuffer(nextLine));//change#1
        nextLine = getLine();
    }

    //get the lines
    while( //the collection has more elements )
        Iterator iterator = hashSet.iterator();
        String unitData = null;
        unitData = new String (  (StringBuffer)( iterator.next() )
);//change#2
        //tokenize the String, extract the consumption and
        //add it to a data structure for later output.
        //no code changes after this point
    }

As a reminder, the program ran perfectly when the change#1 and change#2
were written as follows:

#1    hashSet.add(nextLine);
#2    unitData = iterator.next().toString();

Any help as to why StringBuffer produces bad sums is greatly
appreciated.
Silvio Bierman - 15 Mar 2005 18:46 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.