Issue
Assume that I have a list of Strings.
List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");
When I use StringUtils.join(",", s)
it gives me the result as
"one, two, three"
Whereas I need the output as
"one","two","three"
We don't like to use Guava utility as the project is not in active state.
Is it possible via Apache Commons utility?
How can I achieve this via utility instead of writing my own logic to do the same?
Solution
You can do it in two steps with StringUtils
only,
List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");
String step1 = StringUtils.join(s, "\", \"");// Join with ", "
String step2 = StringUtils.wrap(step1, "\"");// Wrap step1 with "
System.out.println(step2);
Output,
"one", "two", "three"
BUT
I need to pass them in a mongo DB query when using $in operator
For mongodb
query you don't need to build it this way, specifically in case of $in
you can query documents in following way,
BasicDBObject yourInQuery = new BasicDBObject();
yourInQuery.put("in_column", new BasicDBObject("$in", yourList));
DBCursor cursor = collection.find(yourInQuery);
Please read more about this in following link,
Answered By - akash
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)