将数字字符串转换为整数列表[重复](Converting String of digits to List of integer [duplicate])
这个问题在这里已有答案:
如何将Java 8 IntStream转换为List? 3个答案我正在做一些关于java 8流功能的练习,所以想到应用知识的问题将数字字符串转换为整数列表
典型的测试看起来像
@Test public void testGetListofIntegersFromString(){ List<Integer> result = getIntegers("123456780"); assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,0),result); }我写了下面的方法
List<Integer> getIntegers(String value) { return IntStream.rangeClosed(0, value.length() - 1).map(i -> Integer.valueOf(value.substring(i,i+1))).collect(?????); }我被困在哪个函数用于获取我尝试collect(Collectors.toList())的整数列表collect(Collectors.toList())它给出编译错误。
请建议我们是否可以遵循不同的解决方案。
This question already has an answer here:
How do I convert a Java 8 IntStream to a List? 4 answersI am doing some hands on exercise on java 8 stream features so thought of applying the knowledge with the problem Converting String of digits to List of integer
a typical test would look like
@Test public void testGetListofIntegersFromString(){ List<Integer> result = getIntegers("123456780"); assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,0),result); }I have written below method
List<Integer> getIntegers(String value) { return IntStream.rangeClosed(0, value.length() - 1).map(i -> Integer.valueOf(value.substring(i,i+1))).collect(?????); }I am stuck about which function to use to get The List Of Integers I tried collect(Collectors.toList()) Its giving compilation error.
Please suggest if we can follow different to solve this .
最满意答案
使用String.chars():
"123456780".chars().map(c -> c-'0').boxed().collect(Collectors.toList());Use String.chars():
"123456780".chars().map(c -> c-'0').boxed().collect(Collectors.toList());更多推荐
发布评论