GeeCON Prague 2014*
The moose likes Java in General and the fly likes List to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "List to string" Watch "List to string" New topic
Author

List to string

Joe Stela
Greenhorn

Joined: Mar 22, 2012
Posts: 16
Hi,

I have a list and I need to pass this to servlet without []. Currently it is going as ["123" , "454" , "398"]. But I want to pass it as - "123" , "454" , "398". How can I convert this list to comma separated string?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Build the string yourself. Iterate over the list and append each element to a StringBuffer as you go.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Personally I'm a big fan of Google Guava, and its Joiner class handles this problem quickly and elegantly. Of course if you've never written code to do this yourself, it's certainly worthwhile to know how to do it - it's quite simple really, and if it doesn't seem simple, it's worth taking some time to work it out. So you don't really need Guava for this. But Guava is chock full of other useful classes, and so it's worth downloading it and becoming familiar with its classes and methods just on principal. You'll incidentally get Joiner along the way.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Mike Simmons wrote:Personally I'm a big fan of Google Guava, and its Joiner class...

Well don't I feel the fool now. I wrote a utility class a while back that does pretty much the same thing.

Thanks for the link...dammit...

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40



Another option, since the toString() method of the list is consistent, is to just take a substring() to remove the square brackets.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:

Another option, since the toString() method of the list is consistent, is to just take a substring() to remove the square brackets.

Henry


I have to say I would advise against that, on the principle of not relying on the format or a toString() result.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, that format is documented, for any subclass of AbstractCollection. So I'd say it's safe to rely on this as long as you control what instance of List is being used, and can see that it uses AbstractCollection. If this is a utility method that could take some other List type, then we can't assume anything about the toString() implementation.
Joe Stela
Greenhorn

Joined: Mar 22, 2012
Posts: 16
thanks for the reply..I think one of the solution will solve my problem.

I want try out these solutions but I got stuck with one more prob. I want to get the employee no from selected checkbox,but I am getting it as null .

Below is my code.

I am selecting multiple employees from check box and then clicking on one button. The button has clickFunction() as below which is just submitting the form.


function clickFunction(){
$('#employeeForm').submit();
}

After submit I am going to one jsp and trying to get the checkbox selected values in that jsp using request.getParameterValues("employeeList");
employeeList is name of my checkbox.

But I am getting null. Please let me know if I am missing something.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Mike Simmons wrote:Well, that format is documented, for any subclass of AbstractCollection. So I'd say it's safe to rely on this as long as you control what instance of List is being used, and can see that it uses AbstractCollection.

And doesn't override its toString() method. And therein lies the rub, methinks - unless you're an advocate of the dread reflection (blecch, ptui, ptui...spittoon please).

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Joe Stela wrote:function clickFunction(){
$('#employeeForm').submit();
}

Don't look like no Java I've ever come across. Javascript perhaps?

Despite their similarities, the two are NOT the same.

Winston
Joe Stela
Greenhorn

Joined: Mar 22, 2012
Posts: 16
I am trying to submit the Form using jquery submit and employeeForm is the id of form field
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Winston Gutkowski wrote:
Mike Simmons wrote:Well, that format is documented, for any subclass of AbstractCollection. So I'd say it's safe to rely on this as long as you control what instance of List is being used, and can see that it uses AbstractCollection.

And doesn't override its toString() method.

Technically AbstractCollection has given a contract that should apply to any overriding class. Of course we know that implementations can break this contract - but that's their fault. Elsewhere in the API they take pains to add clauses like "the foo() method for class Bar" or "this implementation of foo()" when they want to add implementation-specific notes about details that are allowed to change in overriding implementations. E.g. see the API for Object's equals() or toString(), which detail the general contracts of these methods, and then add some implementation-specific notes introduced by, for example, "The toString method for class Object...". Or if you look at many other methods in AbstractCollection, they say "this implementation..." before anything not in the general contract. In contrast AbstractCollection's toString() does nothing of the sort - everything it says is part of the contract. Overriding methods may do something to achieve the specified format more efficiently, but they are not permitted to change the format.

Winston Gutkowski wrote:And therein lies the rub, methinks - unless you're an advocate of the dread reflection (blecch, ptui, ptui...spittoon please).

I'm not as reflection-averse as you are - I approve of various tools that use reflection under the covers, for example. But I see no reason to bring in reflection here. I already said this only works if you control the specific type of the List being used, in which case what would we need reflection for? Just look at the API.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Joe Stela wrote:I am trying to submit the Form using jquery submit and employeeForm is the id of form field

jQuery probably fits best under the JavaScript forum. Definitely not in "Java in General". This second question seems unrelated to the first, so it probably deserves a separate thread.
Joe Stela
Greenhorn

Joined: Mar 22, 2012
Posts: 16
Iterating and then appending to string buffer worked for me.
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: List to string