This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Aliter for string formatting - Which is best approach? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Aliter for string formatting - Which is best approach?" Watch "Aliter for string formatting - Which is best approach?" New topic
Author

Aliter for string formatting - Which is best approach?

zahid zubair
Ranch Hand

Joined: Aug 29, 2009
Posts: 32
[size=12][color=black]Hi,

I am right now looking for a best alternative for the below approach:

I need to format a set of values from DB to be separated with a pipe('|') delimiter (for each record). Right now I am doing string concatenations as such:



I think this approach is very time consuming because of the number of string operations.
Could anyone suggest me the best approach to achieve the same task? I believe we can use pattern for this. Appreciate your help.


Thanks,
Zahid[/color][/size]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

zahid zubair wrote:
I think this approach is very time consuming because of the number of string operations.
Could anyone suggest me the best approach to achieve the same task? I believe we can use pattern for this. Appreciate your help.


I wouldn't worry about this too much. The Java compiler will generate code using a StringBuilder. So, this...



Will be changed to something like this...



Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or use Apache's StringUtils.join(collection, "|") if it's in a collection.
zahid zubair
Ranch Hand

Joined: Aug 29, 2009
Posts: 32
Hi Henry/David,

Thanks for your suggestions.

Henry - I feel this is time consuming because This piece of code would execute more than 10,000 times (or may be more than that) per day. Also, my approach as well as your approach are losing readability. I want my application to be more readable and more efficient.

Could you guys help me on this.

David,
I am picking up values from the DB. Actually I want to read the values from DB and make it avbl in a .txt file. Can I write my data row by row or is it best to write it at one shot into the file once I get out of the for loop?

Thanks for your help.

Thanks,
Zahid
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

zahid zubair wrote:Henry - I feel this is time consuming because This piece of code would execute more than 10,000 times (or may be more than that) per day. Also, my approach as well as your approach are losing readability. I want my application to be more readable and more efficient.


First, it is *not* my approach. I was just showing you what the compiler does under the covers. As you can see, only one stringbuilder is created in the interim -- there are no interim strings created from that long chain. There isn't really any way to make it more efficient than that... except of course, to not have all those strings, to be concatenated, in the first place.

As for making it more readable.... well, you can separate the lines (and comment). It doesn't make the compiler less efficient by doing so...



Henry
 
wood burning stoves
 
subject: Aliter for string formatting - Which is best approach?
 
Similar Threads
Multiple Threads Accessing Multiple Methods Independently
doubt about loading classes
? - Is there a way to validate 1 XML record at a time, using the SAX or other parser!
Struts validate - how-to repopulate lists, bean:write, etc. after validation fails
How to set the fontcolor and size for the selected text of text area ?