如何将Double列表转换为String列表?

Ras*_*mus 6 java string double list

对于你们所有人来说这可能太容易了,但我只是在一个项目中学习和实现Java而且我坚持这个.

如何转换ListDoubleList String

alp*_*ian 9

有很多方法可以做到这一点,但这里有两种样式供您选择:

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());
}
Run Code Online (Sandbox Code Playgroud)

但更酷的方法是使用现代集合API(我最喜欢的是Guava)并以更实用的方式执行此操作:

List<String> strings = Lists.transform(ds, new Function<Double, String>() {
        @Override
        public String apply(Double from) {
            return from.toString();
        }
    });
Run Code Online (Sandbox Code Playgroud)


Tho*_*lut 6

您必须迭代双列表并添加到新的字符串列表.

List<String> stringList = new LinkedList<String>();
for(Double d : YOUR_DOUBLE_LIST){
   stringList.add(d.toString());
}
return stringList;
Run Code Online (Sandbox Code Playgroud)


小智 5

List<Double> ds = new ArrayList<>();
// fill ds with Doubles
List<String> strings = ds.stream().map(String::valueOf).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)