Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Bhagat Singh Rawat wrote:Its homework in terms of performance, I have 3 solutions: ...
There is really only one way to do this: go through all the values in the set and concatenate them together. All three of your solutions come down to the same, there will not be a major performance difference between them. I bet that if you look up the source code of the StringUtils method from Apache Commons, it will be very similar to your first solution.
Performance-wise, you're already doing the right thing in your own first solution in that you are using a StringBuilder to concatenate all the parts together, instead of using String objects and concatenating them with the + operator (that would make a lot of unnecessary temporary String objects and unnecessary copying of data).
By the way, this is not really a question about the SCJP. I will move this topic to a more appropriate forum for you.
creates two Strings. Also the first solution leaves all the three objects unreferenced on the heap (a memory wast?) whereas the last solution only leaves one unreferenced String.
Correct me if I'm wrong.
Technically you are right. However, the String returned by String.substring shares the string data (a char) with the original String. So yes, there is a little "waist", but that amounts to one object reference with a few primitive fields. Nothing to worry about unless resources are scarce.