How to convert a list of two to a list of strings?

It may be too easy for all of you, but I'm just learning and embedding Java in a project and am stuck with it.

How to convert List from Double to List String ?

+6
source share
4 answers

There are many ways to do this, but here you can choose two styles:

 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 the cooler way to do this is to use the modern collection API (my favorite Guava ) and make it into a more functional style:

 List<String> strings = Lists.transform(ds, new Function<Double, String>() { @Override public String apply(Double from) { return from.toString(); } }); 
+9
source

You need to iterate over your double list and add to the new list of lines.

 List<String> stringList = new LinkedList<String>(); for(Double d : YOUR_DOUBLE_LIST){ stringList.add(d.toString()); } return stringList; 
+6
source
 List<Double> doubleList = new ArrayList<Double>(); doubleList.add(1.1d); doubleList.add(2.2d); doubleList.add(3.3d); List<String> listOfStrings = new ArrayList<String>(); for (Double d:doubleList) listOfStrings.add(d.toString()); 
+1
source
 List<Double> ds = new ArrayList<Double>(); // fill ds with Doubles List<String> strings = ds.stream().map(op -> op.toString()).collect(Collectors.toList()); 
+1
source

Source: https://habr.com/ru/post/889615/


All Articles