2 Replies - 218 Views - Last Post: 21 April 2011 - 01:17 PM Rate Topic: -----

#1 Riv3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 29-January 10

Best Performance With String Concatenation

Posted 21 April 2011 - 11:24 AM

Hey guys, I have a project that I'm working on where I have to traverse through a linked list of strings and concatenate all of the strings together. Now, I know that strings are immutable and really slow, so I was wondering if there was a way that I could improve my performance. I did some research and came across the StringBuilder class, but I'm still curious if there is a faster way.

public String toString() {
		Node temp = head;
		StringBuilder value = new StringBuilder();
		while (temp != null) {
			value.append(temp.toString());
			temp = temp.next;
		}
		return value.toString();
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Best Performance With String Concatenation

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Best Performance With String Concatenation

Posted 21 April 2011 - 12:23 PM

That may be cool, have you tried it to see if it does what you want?
I the issue is concatenating string, try these documentation and pages:
Class String
String

This post has been edited by smohd: 21 April 2011 - 12:30 PM

Was This Post Helpful? -1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Best Performance With String Concatenation

Posted 21 April 2011 - 01:17 PM

The StringBuilder is your best way of concatenation. If you are working with multiple Threads, stick with StringBuffer, as it is Thread-safe.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1