Java 使用流将字符串转换为ArrayList
我需要将字符串转换为ArrayListJava 使用流将字符串转换为ArrayList,java,java-8,java-stream,Java,Java 8,Java Stream,我需要将字符串转换为ArrayList String accountNumberValue = "151616165132132"; 我尝试这样做,但它看起来像是硬编码的,通过双重解析char到String和Integer: 有什么简单的方法吗?您可以使用拆分进行单个解析: 也许改进不大,但您可以使用CharactergetNumericValue: 你可以这样做: List<Integer> result=accountNumberValue
String accountNumberValue = "151616165132132";
我尝试这样做,但它看起来像是硬编码的,通过双重解析char到String和Integer:
有什么简单的方法吗?您可以使用拆分进行单个解析:
也许改进不大,但您可以使用CharactergetNumericValue:
你可以这样做:
List<Integer> result=accountNumberValue
.chars() //Get IntStream from a string with char codes
.map(Character::getNumericValue) //Map to the actual int
.boxed() //Box the intstream
.collect(Collectors.toList()); //Collect
什么的阵列列表?以什么形式?我在发布我的答案后看到了你的答案。这几乎是一样的,但不起作用,因为char返回IntStream,所以为了收集它,您需要先“装箱”它。如果你测试它甚至不编译
List<Integer> accNumArray =
Arrays.stream(accountNumberValue.split(""))
.map(Integer::parseInt)
.collect(Collectors.toList());
List<Integer> accNumArray = accountNumberValue.chars()
.map(c -> new Integer(Character.getNumericValue(c)))
.collect(Collectors.toList());
List<Integer> result=accountNumberValue
.chars() //Get IntStream from a string with char codes
.map(Character::getNumericValue) //Map to the actual int
.boxed() //Box the intstream
.collect(Collectors.toList()); //Collect