如何在使用流的同时将长期转换为bigdecimal
我正在努力了解如何使以下代码工作。
我的 stat类
的字段 count_human_dna
是类型 bigdecimal
,将类型设置为the The This Jung,但我需要将其更改为<代码> bigdecimal ,能否以某种方式告诉我如何为 bigdecimal
字段做这项工作?
stat.setCount_human_dna(dnaSamples.stream()
.filter(x -> x.getType().equals("Human"))
.collect(Collectors.counting()));
此代码正在计算所有属于人类的 dnasamples
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
bigdecimal#valueof
方法,用于从长
转换为bigdecimal
。请参阅以获取更多详细信息。
Use the
BigDecimal#valueOf
method for the conversion fromlong
toBigDecimal
.See the JavaDocs for more detail.
最简单,最有效的方法是使用终端操作
count> count()
返回流中元素的数量为long
,然后转换进入bigdecimal
:您可以使用 bigdecimal 的结果javase/17/docs/api/java.base/java/utif/stream/stream.html#redion(u,java.util.function.bifunction,java.util.function.binarection.binareoperator)“
dred> redain
终端操作:sidenote:我不是DNA分析等问题的专家,但是此减少的结果将始终是一个整数。您可以考虑使用
biginteger
而不是bigdecimal
。The most simple and efficient way to do is to use terminal operation
count()
which returns the number of elements in the stream aslong
and then convert intoBigDecimal
:And you can produce the result of type
BigDecimal
directly from the stream usingreduce()
a terminal operation:Sidenote: I'm not an expert in such questions as DNA analysis, but the result of this reduction will always be a whole number. You might consider utilizing
BigInteger
instead ofBigDecimal
.