Converting a List<Long> to comma separated string [duplicate]
Asked Answered
I

1

25

Hi I am having a requirement where I need to convert List of Long datatype to comma separated String. Which would be the appropriate way. I have thought about using string builder or converting it to List<String> and then using StringUtils join to get a String.

I am looking for java 7 solution,Not using guava or java 8.

Illassorted answered 29/10, 2015 at 8:48 Comment(5)
I have tried converting it to list of string and using string utils.join . But i am not sure is this the right way,Illassorted
Okay, that duplicate was about List<String>, not List<Long>, but most answers should still apply. Reopen if not.Westernize
can you refer this, list.toString()Hardee
i have tried TextUtils.join(",", urlist<Long>); and worked for meEnnui
What bout this approach: String collect = listLong.stream().map(x->String.valueOf(x)).collect(Collectors.joining(",")); listLong is reference for List<Long>Eng
J
36

You can try this:

StringUtils.join(mylist, ',');

See org.apache.commons.lang3.StringUtils.

Janel answered 29/10, 2015 at 8:51 Comment(8)
That also works with List<Long>. The method takes an Iterable<?>Westernize
I was under wrong assumption that it will work only for List of String Data Types, ThanksIllassorted
@JordiCastilla - I know. But I presume that some other people who will be viewing this answer might be using java -8 :)Becker
@VinodMadyalkar: and these other people will soon find out that StringJoiner does not work with List<Long>.Westernize
@Westernize - Ya, they will have to cast longs to strings in some way. Hmm.. Probably not worth mentioning it at all thenBecker
It prepended separator when used on Set<Long>Iceland
issue I am facing is , values IN ( ? ) , It should prepare (v1,v2) but it creates ('v1,v2') , any alternative solutions ?Ongoing
It worked with org.apache.commons.lang3.StringUtils import, but NOT with com.sun.deploy.util.StringUtils.None

© 2022 - 2024 — McMap. All rights reserved.