• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to get data from Set in one go

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hard to tell unless you actually clock it. Any algorithm would take atleast O(N) time.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that there's a very easy way to double the performance of method #2 -- but I'll leave that to your imagination...
 
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow... took me 4 tries to post that. The boards are really acting up today
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rob Spoor
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'm still in control here. LOOK at this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic