Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List to string

 
Joe Stela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Build the string yourself. Iterate over the list and append each element to a StringBuffer as you go.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to submit the Form using jquery submit and employeeForm is the id of form field
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterating and then appending to string buffer worked for me.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic