Sums and StringBuffers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • guyzdancin

    #1

    Sums and StringBuffers

    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(ne xtLine));//change#1
    nextLine = getLine();
    }


    //get the lines
    while( //the collection has more elements )
    Iterator iterator = hashSet.iterato r();
    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(nex tLine);
    #2 unitData = iterator.next() .toString();

    Any help as to why StringBuffer produces bad sums is greatly
    appreciated.

  • Silvio Bierman

    #2
    Re: Sums and StringBuffers


    "guyzdancin " <massmail@guysu ssman.com> wrote in message
    news:1110903695 .611053.219510@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > 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(ne xtLine));//change#1
    > nextLine = getLine();
    > }
    >
    >
    > //get the lines
    > while( //the collection has more elements )
    > Iterator iterator = hashSet.iterato r();
    > 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(nex tLine);
    > #2 unitData = iterator.next() .toString();
    >
    > Any help as to why StringBuffer produces bad sums is greatly
    > appreciated.
    >[/color]

    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


    Comment

    • shriop

      #3
      Re: Sums and StringBuffers

      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.

      Comment

      Working...