java 如何将双精度列表转换为字符串列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6213794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert List of Double to List of String?
提问by Rasmus
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.
这对你们所有人来说可能太容易了,但我只是在一个项目中学习和实现 Java 并且坚持这个。
How to convert List
of Double
to List
String
?
如何转换List
的Double
到List
String
?
回答by alpian
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:
但一个更酷的方法是使用现代集合 API(我最喜欢的是Guava)并以更实用的方式执行此操作:
List<String> strings = Lists.transform(ds, new Function<Double, String>() {
@Override
public String apply(Double from) {
return from.toString();
}
});
回答by Thomas Jungblut
You have to iterate over your double list and add to a new list of strings.
您必须遍历双列表并添加到新的字符串列表中。
List<String> stringList = new LinkedList<String>();
for(Double d : YOUR_DOUBLE_LIST){
stringList.add(d.toString());
}
return stringList;
回答by Mob
List<Double> ds = new ArrayList<Double>();
// fill ds with Doubles
List<String> strings = ds.stream().map(op -> op.toString()).collect(Collectors.toList());
回答by Erhan Bagdemir
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());