我正在努力为此获得一个功能代码。
我有一个 0 到 9 之间的数字流。我想从这些数字中得到一个 BigInteger
。
示例:
IntStream digits = IntStream.of(1, 2, 3) // should get me a Biginteger 123.
IntStream digits = IntStream.of(9, 5, 3) // should get me a Biginteger 953.
有没有办法将流中的所有元素连接起来? 这是我的基本想法:
digits.forEach(element -> result=result.concat(result, element.toString()));
最佳答案
您可以将每个数字映射到一个字符串,将它们连接在一起,然后从中创建一个 BigInteger
:
BigInteger result =
IntStream.of(1, 2, 3)
.mapToObj(String::valueOf)
.collect(Collectors.collectingAndThen(Collectors.joining(),
BigInteger::new));
关于Java Stream 数字到数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44344650/