This just might be too easy for all of you, but I am just learning and implementing Java in a project and am stuck with this.
How to convert List
of Double
to List
String
?
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
We can use StringBuilder class to convert List to String. StringBuilder is the best approach if you have other than String Array, List. We can add elements in the object of StringBuilder using the append() method while looping and then convert it into string using toString() method of String class at the end.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
There are many ways to do this but here are two styles for you to choose from:
List<Double> ds = new ArrayList<Double>();
// fill ds with Doubles
List<String> strings = new ArrayList<String>();
for (Double d : ds) {
// Apply formatting to the string if necessary
strings.add(d.toString());
}
But a cooler way to do this is to use a modern collections API (my favourite is Guava) and do this in a more functional style:
List<String> strings = Lists.transform(ds, new Function<Double, String>() {
@Override
public String apply(Double from) {
return from.toString();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With