Java 将数组/字符串列表转换为数组/整数列表的 Lambda 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23057549/
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
Lambda expression to convert array/List of String to array/List of Integers
提问by Reddy
Since Java 8 comes with powerful lambda expressions,
由于 Java 8 带有强大的 lambda 表达式,
I would like to write a function to convert a List/array of Strings to array/List of Integers, Floats, Doubles etc..
我想编写一个函数来将字符串列表/数组转换为整数、浮点数、双精度数等数组/列表。
In normal Java, it would be as simple as
在普通的 Java 中,它会像
for(String str : strList){
intList.add(Integer.valueOf(str));
}
But how do I achieve the same with a lambda, given an array of Strings to be converted to an array of Integers.
但是,给定要转换为整数数组的字符串数组,我如何使用 lambda 实现相同的效果。
采纳答案by Alexis C.
You could create helper methods that would convert a list (array) of type T
to a list (array) of type U
using the map
operation on stream
.
您可以创建辅助方法,使用on 操作T
将类型列表(数组)转换为类型列表(数组)。U
map
stream
//for lists
public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
return from.stream().map(func).collect(Collectors.toList());
}
//for arrays
public static <T, U> U[] convertArray(T[] from,
Function<T, U> func,
IntFunction<U[]> generator) {
return Arrays.stream(from).map(func).toArray(generator);
}
And use it like this:
并像这样使用它:
//for lists
List<String> stringList = Arrays.asList("1","2","3");
List<Integer> integerList = convertList(stringList, s -> Integer.parseInt(s));
//for arrays
String[] stringArr = {"1","2","3"};
Double[] doubleArr = convertArray(stringArr, Double::parseDouble, Double[]::new);
请注意,
s -> Integer.parseInt(s)
s -> Integer.parseInt(s)
可以替换为Integer::parseInt
Integer::parseInt
(请参阅Method references方法参考)回答by ZehnVon12
List<Integer> intList = strList.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
回答by Colander
EDIT:
As pointed out in the comments, this is a much simpler version:
Arrays.stream(stringArray).mapToInt(Integer::parseInt).toArray()
This way we can skip the whole conversion to and from a list.
编辑:正如评论中所指出的,这是一个更简单的版本:
Arrays.stream(stringArray).mapToInt(Integer::parseInt).toArray()
这样我们就可以跳过与列表之间的整个转换。
I found another one line solution, but it's still pretty slow (takes about 100 times longer than a for cycle - tested on an array of 6000 0's)
我找到了另一个单行解决方案,但它仍然很慢(比 for 周期长大约 100 倍 - 在 6000 个 0 的数组上测试)
String[] stringArray = ...
int[] out= Arrays.asList(stringArray).stream().map(Integer::parseInt).mapToInt(i->i).toArray();
What this does:
这是做什么的:
- Arrays.asList() converts the array to a List
- .stream converts it to a Stream (needed to perform a map)
- .map(Integer::parseInt) converts all the elements in the stream to Integers
- .mapToInt(i->i) converts all the Integers to ints (you don't have to do this if you only want Integers)
- .toArray() converts the Stream back to an array
- Arrays.asList() 将数组转换为列表
- .stream 将其转换为 Stream(需要执行映射)
- .map(Integer::parseInt) 将流中的所有元素转换为整数
- .mapToInt(i->i) 将所有整数转换为整数(如果您只想要整数,则不必这样做)
- .toArray() 将 Stream 转换回数组
回答by Stan Kurdziel
The helper methods from the accepted answer are not needed. Streamscan be used with lambdas or usually shortened using Method References. Streams enable functional operations. map()
converts the elements and collect(...)
or toArray()
wrap the stream back up into an array or collection.
不需要接受的答案中的辅助方法。Streams可以与 lambdas 一起使用,或者通常使用Method References缩短。流支持功能性操作。map()
转换元素和/collect(...)
或toArray()
将流包装回数组或集合。
Venkat Subramaniam's talk (video)explains it better than me.
Venkat Subramaniam 的演讲(视频)比我解释得更好。
1 Convert List<String>
to List<Integer>
1 转换List<String>
为List<Integer>
List<String> l1 = Arrays.asList("1", "2", "3");
List<Integer> r1 = l1.stream().map(Integer::parseInt).collect(Collectors.toList());
// the longer full lambda version:
List<Integer> r1 = l1.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
2 Convert List<String>
to int[]
2 转换List<String>
为int[]
int[] r2 = l1.stream().mapToInt(Integer::parseInt).toArray();
3 Convert String[]
to List<Integer>
3 转换String[]
为List<Integer>
String[] a1 = {"4", "5", "6"};
List<Integer> r3 = Stream.of(a1).map(Integer::parseInt).collect(Collectors.toList());
4 Convert String[]
to int[]
4 转换String[]
为int[]
int[] r4 = Stream.of(a1).mapToInt(Integer::parseInt).toArray();
5 Convert String[]
to List<Double>
5 转换String[]
为List<Double>
List<Double> r5 = Stream.of(a1).map(Double::parseDouble).collect(Collectors.toList());
6 (bonus) Convert int[]
to String[]
6(奖金)转换int[]
为String[]
int[] a2 = {7, 8, 9};
String[] r6 = Arrays.stream(a2).mapToObj(Integer::toString).toArray(String[]::new);
Lots more variations are possible of course.
当然,更多的变化是可能的。
Also see Ideone version of these examples. Can click fork and then run to run in the browser.
另请参阅这些示例的 Ideone 版本。可以点击fork然后运行在浏览器中运行。
回答by Zhurov Konstantin
In addition - control when string array doesn't have elements:
另外 - 当字符串数组没有元素时控制:
Arrays.stream(from).filter(t -> (t != null)&&!("".equals(t))).map(func).toArray(generator)
回答by user2182920
You can also use,
您还可以使用,
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
Integer[] array = list.stream()
.map( v -> Integer.valueOf(v))
.toArray(Integer[]::new);
回答by Sahil Chhabra
For List :
对于列表:
List<Integer> intList
= stringList.stream().map(Integer::valueOf).collect(Collectors.toList());
For Array :
对于数组:
int[] intArray = Arrays.stream(stringArray).mapToInt(Integer::valueOf).toArray();
回答by Praveen Codur
Arrays.toString(int []) works for me.
Arrays.toString(int []) 对我有用。
回答by LeoLozes
I didn't find it in the previous answers, so, with Java 8 and streams:
我在之前的答案中没有找到它,因此,对于 Java 8 和流:
Convert String[]
to Integer[]
:
转换String[]
为Integer[]
:
Arrays.stream(stringArray).map(Integer::valueOf).toArray(Integer[]::new)
回答by Deep Lotia
I used maptoInt() with Lambda operation for converting string to Integer
我使用 maptoInt() 和 Lambda 操作将字符串转换为整数
int[] arr = Arrays.stream(stringArray).mapToInt(item -> Integer.parseInt(item)).toArray();