java - 将 List<Integer> 转换为 List<String>

标签 java string collections integer

我有一个整数列表,List<Integer>我想将所有整数对象转换为字符串,从而完成一个新的List<String> .

当然,我可以创建一个新的 List<String>并循环调用列表 String.valueOf()对于每个整数,但我想知道是否有更好的(阅读:更自动)方法?

最佳答案

使用 Google Collections from Guava-Project ,您可以使用 Lists 中的 transform 方法类

import com.google.common.collect.Lists;
import com.google.common.base.Functions

List<Integer> integers = Arrays.asList(1, 2, 3, 4);

List<String> strings = Lists.transform(integers, Functions.toStringFunction());

transform 返回的 List 是后备列表上的 view - 转换将应用于对已转换列表的每次访问。

请注意,Functions.toStringFunction() 在应用于 null 时会抛出 NullPointerException,因此只有在您确定您的列表不包含 null 时才使用它。

关于java - 将 List<Integer> 转换为 List<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524/

相关文章:

java - 使用索引值从 HashBasedTable(Guava 集合)中提取数据

java - 选择可能的最大数字

Java返回值取决于字符串中猫和狗的出现?

java - 是否可以使用默认初始化过程在 EJB 模块中初始化 log4j?

swift - 如何比较swift 4中的两个字符串索引

c++ - 如何实现仅在堆栈上分配的字符串

c# - 在 C# 中处理可变集合键

java - 从 ArrayList 中删除偶数

java - "Abrupt completion of a finally clause"到底是什么?

java - 编写高效的 Java 代码是否需要深入了解底层?