将数字字符串转换为整数列表[重复](Converting String of digits to List of integer [duplicate])

系统教程 行业动态 更新时间:2024-06-14 17:01:31
将数字字符串转换为整数列表[重复](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 answers

I 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());

更多推荐

本文发布于:2023-04-20 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e0ea5af326f98b22e85f69412be7d0d9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   字符串   转换为   数字   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!