• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

List to string

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Build the string yourself. Iterate over the list and append each element to a StringBuffer as you go.
 
Rancher
Posts: 4327
59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterating and then appending to string buffer worked for me.
Thanks
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic