| Author |
How to get data from Set in one go
|
Bhagat Singh Rawat
Ranch Hand
Joined: Apr 04, 2009
Posts: 93
|
|
Hi Ranchers,
I want to get all values of a Set interface in one go in Comma Separated String.
For Example:
if I print Set as value.toString then the output would be:
But my requirement is
Apple, Banana, Orange
without [] squire brackets.
Please suggest.
|
Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
|
 |
armando fonseca
Ranch Hand
Joined: Apr 03, 2009
Posts: 49
|
|
is this a homework? or an assignment ? there is a policy about question regarding those scenario. You can use String methods, or regex if you are comfortable with it.
|
scjp6-90%
|
 |
Bhagat Singh Rawat
Ranch Hand
Joined: Apr 04, 2009
Posts: 93
|
|
armando fonseca wrote:is this a homework? or an assignment ? there is a policy about question regarding those scenario. You can use String methods, or regex if you are comfortable with it.
Its homework in terms of performance, I have 3 solutions:
1-
2-
fruits.toString().substring(1, fruits.toString().length - 1);
3-
String allFruits = StringUtils.join(fruits, ", ");
Note: StringUtils is class from Apache Commons Lang Library
Now you tell me which one is good in terms of performance?
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6603
|
|
|
Hard to tell unless you actually clock it. Any algorithm would take atleast O(N) time.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
Note that there's a very easy way to double the performance of method #2 -- but I'll leave that to your imagination...
|
[Jess in Action][AskingGoodQuestions]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Bhagat Singh Rawat wrote:1-
That's more or less what Set's toString does (or actually AbstractCollection):
I agree that using your while code looks much nicer than this though.
And to quote Campbell: never use "== true" or "== false". You risk using an assignment if you omit one of the = symbols.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Bhagat Singh Rawat
Ranch Hand
Joined: Apr 04, 2009
Posts: 93
|
|
Ernest Friedman-Hill wrote:Note that there's a very easy way to double the performance of method #2 -- but I'll leave that to your imagination...
Humm, I think that is out of reach . Please tell me how can I do that.
|
 |
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
|
|
I would imagine is has something to do with you calling "fruits.toString()" 2 times in the same method call. A doubling of performance would occur if you could somehow omit one
|
SCJA
~Currently preparing for SCJP6
|
 |
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
|
|
wow... took me 4 tries to post that. The boards are really acting up today
|
 |
Bhagat Singh Rawat
Ranch Hand
Joined: Apr 04, 2009
Posts: 93
|
|
Brian Legg wrote:I would imagine is has something to do with you calling "fruits.toString()" 2 times in the same method call. A doubling of performance would occur if you could somehow omit one
You mean if I change my code as
would be faster than the earlier code?
|
 |
David Marco
Ranch Hand
Joined: Feb 23, 2009
Posts: 44
|
|
fruits.toString().substring(1, fruits.toString().length - 1);
above code appears to create three string objects "from side to side" whereas
1. String str = fruits.toString();
2. str = str.substring(1, str.length - 1);
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.
|
SCJP 6
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
David Marco wrote:
fruits.toString().substring(1, fruits.toString().length - 1);
above code appears to create three string objects "from side to side" whereas
1. String str = fruits.toString();
2. str = str.substring(1, str.length - 1);
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.
|
 |
 |
|
|
subject: How to get data from Set in one go
|
|
|