aspose file tools*
The moose likes Beginning Java and the fly likes How to get data from Set in one go Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get data from Set in one go" Watch "How to get data from Set in one go" New topic
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: 6661
    
    5

Hard to tell unless you actually clock it. Any algorithm would take atleast O(N) time.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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: 24183
    
  34

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: 19672
    
  18

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 - OCEEJBD 6
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: 19672
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get data from Set in one go