首页 / 知识
关于java:将List <Integer>转换为List <String>
2023-04-12 15:54:00

Converting List
|
1 2 3 4 5 6 | 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()); |
请注意,将
据我所知,迭代和实例化是实现此目的的唯一方法。诸如此类的东西(对于其他潜在的帮助,因为我确定您知道该怎么做):
1 2 3 4 5 6 | List<Integer> oldList = ... /* Specify the size of the list up front to prevent resizing. */ List<String> newList = new ArrayList<String>(oldList.size()) for (Integer myInt : oldList) { newList.add(String.valueOf(myInt)); } |
Java 8.的解决方案比Guava的解决方案长一点,但是至少您不必安装库。
1 2 3 4 5 6 7 8 9 | import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; //... List<Integer> integers = Arrays.asList(1, 2, 3, 4); List<String> strings = integers.stream().map(Object::toString) .collect(Collectors.toList()); |
您正在做的事情很好,但是如果您需要" Java-it-up",则可以使用Transformer和Apache Commons的collect方法,例如:
1 2 3 4 5 | public class IntegerToStringTransformer implements Transformer<Integer, String> { public String transform(final Integer i) { return (i == null ? null : i.toString()); } } |
..接着..
1 2 3 4 | CollectionUtils.collect( collectionOfIntegers, new IntegerToStringTransformer(), newCollectionOfStrings); |
而不是使用String.valueOf我会使用.toString();它避免了@ johnathan.holland描述的一些自动装箱
Javadoc说valueOf返回与Integer.toString()相同的东西。
1 2 3 4 5 6 | List<Integer> oldList = ... List<String> newList = new ArrayList<String>(oldList.size()); for (Integer myInt : oldList) { newList.add(myInt.toString()); } |
这是一种单行解决方案,无需与非JDK库作弊。
1 | List<String> strings = Arrays.asList(list.toString().replaceAll("\\[(.*)\\]","$1").split(",")); |
String.valueOf的源显示如下:
1 2 3 | public static String valueOf(Object obj) { return (obj == null) ?"null" : obj.toString(); } |
并不是很重要,但是我会使用toString。
使用Guava和Java 8的另一种解决方案
1 2 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<String> strings = Lists.transform(numbers, number -> String.valueOf(number)); |
不是核心Java,也不是通用化的,但是流行的Jakarta Commons Collections库为此类任务提供了一些有用的抽象。具体来说,看看下面的collect方法
实用工具
如果您已经在项目中使用了commons集合,请考虑一下。
对于那些关心"拳击"的人们,jsight的回答是:没有。在此使用
使用
另外,jsight响应中的一个小缺陷:
Lambdaj允许以非常简单且易读的方式进行操作。例如,假设您有一个Integer列表,并且想要将它们转换为相应的String表示形式,则可以编写类似的代码;
1 2 3 4 | List<Integer> ints = asList(1, 2, 3, 4); Iterator<String> stringIterator = convertIterator(ints, new Converter<Integer, String> { public String convert(Integer i) { return Integer.toString(i); } } |
Lambdaj仅在迭代结果时才应用转换功能。
仅针对专家的答案:
1 2 3 4 | List<Integer> ints = ...; String all = new ArrayList<Integer>(ints).toString(); String[] split = all.substring(1, all.length()-1).split(","); List<String> strs = Arrays.asList(split); |
@乔纳森:我可能会误会,但我相信在这种情况下,String.valueOf()会调用String.valueOf(Object)函数,而不是装箱成String.valueOf(int)。 String.valueOf(Object)如果为null,则仅返回" null";如果非null,则调用Object.toString(),这不应该涉及装箱(尽管显然要实例化新的字符串对象)。
1 | List<String> stringList = integerList.stream().map((Object s)->String.valueOf(s)).collect(Collectors.toList()) |
我认为将Object.toString()用于除调试以外的其他目的可能是一个非常糟糕的主意,即使在这种情况下,两者在功能上是等效的(假设列表中没有空值)。开发人员可以自由更改任何toString()方法的行为,而不会发出任何警告,包括标准库中任何类的toString()方法。
甚至不用担心装箱/拆箱过程导致的性能问题。如果性能至关重要,则只需使用一个阵列即可。如果确实很关键,请不要使用Java。试图超越JVM只会导致心痛。
您无法避免"装箱费用"; Java的虚假通用容器只能存储对象,因此必须将int装箱为Integers。原则上,它可以避免从Object到Integer的贬低(因为它毫无意义,因为Object对于String.valueOf和Object.toString都足够好),但我不知道编译器是否足够聪明。从String到Object的转换应该或多或少是无操作的,因此我不希望为此担心。
这是一件非常基本的事情,我不会使用外部库(这会导致您的项目中可能不需要的依赖项)。
我们有一类专门设计用于完成此类工作的静态方法。因为此代码非常简单,所以让Hotspot为我们进行优化。最近,这似乎是我代码中的主题:编写非常简单(直接)的代码,然后让Hotspot发挥作用。我们很少会遇到类似这样的代码的性能问题-当出现新的VM版本时,您将获得所有额外的速度优势等。
尽管我非常喜欢雅加达(Jakarta)系列,但它们不支持Generics,并使用1.4作为LCD。我对Google Collections保持警惕,因为它们被列为Alpha支持级别!
使用流:
如果说结果是整数列表(
1 | List<String> ids = (List<String>) result.stream().map(intNumber -> Integer.toString(intNumber)).collect(Collectors.toList()); |
解决它的方法之一。希望这可以帮助。
I didn't see any solution which is following the principal of space
complexity. If list of integers has large number of elements then it's
big problem.
1 2 | It will be really good to remove the integer from the List<Integer> and free the space, once it's added to List<String>. |
我们可以使用迭代器来实现相同的目的。
1 2 3 4 5 6 7 8 9 10 11 12 | List<Integer> oldList = new ArrayList<>(); oldList.add(12); oldList.add(14); ....... ....... List<String> newList = new ArrayList<String>(oldList.size()); Iterator<Integer> itr = oldList.iterator(); while(itr.hasNext()){ newList.add(itr.next().toString()); itr.remove(); } |
只是为了好玩,一个应该在JDK7中使用jsr166y fork-join框架的解决方案。
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.concurrent.forkjoin.*; private final ForkJoinExecutor executor = new ForkJoinPool(); ... List<Integer> ints = ...; List<String> strs = ParallelArray.create(ints.size(), Integer.class, executor) .withMapping(new Ops.Op<Integer,String>() { public String op(Integer i) { return String.valueOf(i); }}) .all() .asList(); |
(免责声明:未编译。规范尚未最终)
在JDK7中不太可能使用一些类型推断和语法糖,以使withMapping调用不太冗长:
1 | .withMapping(#(Integer i) String.valueOf(i)) |
我只是想用面向对象的解决方案来解决这个问题。
如果您对域对象建模,则解决方案将在域对象中。这里的域是我们想要字符串值的整数列表。
最简单的方法是根本不转换列表。
话虽这么说,为了不进行转换就将Integer的原始列表更改为Value列表,其中Value看起来像这样...
1 2 3 4 5 6 7 8 9 10 11 | class Value { Integer value; public Integer getInt() { return value; } public String getString() { return String.valueOf(value); } } |
与复制列表相比,这将更快并且占用更少的内存。
祝好运!
最新内容
相关内容
python如何读取列表中元素的位置?
python如何读取列表中元素的位置?,位置,数据,异常,培训,字符串,元素,索引,方法,示例,结果,python读取列表中元素位置的方法:1、使用index()方python中获取路径的三种方法
python中获取路径的三种方法,工作,代码,情况,培训,下来,路径,文件,也就是,桌面,目录,python中获取路径总结下来分为三种情况:1、获取工作目录python列表是否允许重复
python列表是否允许重复,培训,表里,元素,字典,列表,方法,长度,种类,也就是,下面,python列表允许重复,下面我们就来介绍几种判断列表是否重复的python中怎么将元组、字典转化为列
python中怎么将元组、字典转化为列表,培训,列表,字典,方法,元素,示例,中将,语法,以上,参数,python中将元组、字典转化为列表的方法:python中可python如何检测字符串是不是全为字
python如何检测字符串是不是全为字母?,检测,培训,字符串,字母,方法,字符,示例,语法,以上,更多,python中可以使用isalpha()方法检测字符串是不Python文件如何转换为字符串
Python文件如何转换为字符串,数据,主体,名字,培训,文件,字符串,末尾,方法,对象,语句,python文件如何转换为字符串的方法:一、最方便的方法是一Python中区分字符与字符串吗?
Python中区分字符与字符串吗?,数据,培训,数字,字符串,数据类型,字符,类型,语言,有所不同,布尔,python中不区分字符与字符串,与C语言相比,Pythonpython怎么查看列表长度?
python怎么查看列表长度?,代码,技术,培训,列表,长度,方法,示例,元素,鲜为人知,初始化,python中可以使用len()或length_hint()方法查看列表长Python元组怎么转换成列表
Python元组怎么转换成列表,培训,列表,对象,示例,中将,以上,方法,更多,内容,python中将元组转换成列表的方法是使用list()进行转换。使用示例Python如何创建对象,有哪些方法
Python如何创建对象,有哪些方法,培训,对象,方法,内容,函数,属性,所在,以上,以下,功能,以下介绍Python创建对象的9种方法。classPoint:def__inipython中什么是类方法
python中什么是类方法,培训,方法,实例,静态,定义,参数,函数,内部,变量,属性,python中的类方法,指的是在类中定义的函数,函数在类的内部称为方法python看类里有哪些方法
python看类里有哪些方法,信息,员工,培训,名称,方法,函数,冒号,字符串,语句,结尾,python使用class语句来创建一个新类,class之后为类的名称并以