如何在使用流的同时将长期转换为bigdecimal

发布于 2025-01-30 02:28:30 字数 421 浏览 3 评论 0 原文

我正在努力了解如何使以下代码工作。

我的 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

I'm struggling to understand how can I make the following code work.

The field count_human_dna of my stat class is of type BigDecimal, with setting the type as Long this works, but I need to change it to BigDecimal, can somehow tell me how could I make this work for BigDecimal field?

stat.setCount_human_dna(dnaSamples.stream()
    .filter(x -> x.getType().equals("Human"))
    .collect(Collectors.counting()));

This code is counting all the dnaSamples which type belong to Human.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2025-02-06 02:28:30

使用 bigdecimal#valueof 方法,用于从转换为 bigdecimal

stat.setCount_human_dna(BigDecimal.valueOf(dnaSamples.stream().filter(x -> x.getType().equals("Human")).collect(Collectors.counting())));

请参阅以获取更多详细信息。

Use the BigDecimal#valueOf method for the conversion from long to BigDecimal.

stat.setCount_human_dna(BigDecimal.valueOf(dnaSamples.stream().filter(x -> x.getType().equals("Human")).collect(Collectors.counting())));

See the JavaDocs for more detail.

从﹋此江山别 2025-02-06 02:28:30

最简单,最有效的方法是使用终端操作 count> count() 返回流中元素的数量为 long ,然后转换进入 bigdecimal

stat.setCount_human_dna(getDNACount(dnaSamples));
public static BigDecimal getDNACount(Collection<Sample> dnaSamples) {
    long humanSamples = dnaSamples.stream()
        .filter(x -> x.getType().equals("Human"))
        .count();

    return BigDecimal.valueOf(humanSamples);
}

您可以使用 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 终端操作:

stat.setCount_human_dna(getDNACount(dnaSamples));
public static BigDecimal getDNACount(Collection<Sample> dnaSamples) {
    return dnaSamples.stream()
        .filter(x -> x.getType().equals("Human"))
        .reduce(BigDecimal.ZERO,
                (total, next) -> total.add(BigDecimal.ONE),
                BigDecimal::add);
}

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 as long and then convert into BigDecimal:

stat.setCount_human_dna(getDNACount(dnaSamples));
public static BigDecimal getDNACount(Collection<Sample> dnaSamples) {
    long humanSamples = dnaSamples.stream()
        .filter(x -> x.getType().equals("Human"))
        .count();

    return BigDecimal.valueOf(humanSamples);
}

And you can produce the result of type BigDecimal directly from the stream using reduce() a terminal operation:

stat.setCount_human_dna(getDNACount(dnaSamples));
public static BigDecimal getDNACount(Collection<Sample> dnaSamples) {
    return dnaSamples.stream()
        .filter(x -> x.getType().equals("Human"))
        .reduce(BigDecimal.ZERO,
                (total, next) -> total.add(BigDecimal.ONE),
                BigDecimal::add);
}

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 of BigDecimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文