Java DoubleAccumulator accumulate()方法及示例

Java DoubleAccumulator accumulate()方法及示例

Java.DoubleAccumulator.accumulate() 方法是Java中一个内置的方法,它可以更新这个DoubleAccumulator实例中的给定值。这意味着它需要一个双倍的值作为参数,并将其添加到这个DoubleAccumulator实例中,它被调用。

语法

public void accumulate(double valueToBeAccumulated)

参数: 该方法接受一个强制参数 valueToBeAccumulated ,它是这个DoubleAccumulator的当前实例中要更新的双倍值。

返回值: 这个方法不返回任何值。它只是更新这个DoubleAccumulator。

下面的程序演示了这个函数。

程序 1 :

// Java program to demonstrate
// the accumulate() method
 
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create the DoubleAccumulator instance
        DoubleAccumulator num
            = new DoubleAccumulator(Double::sum,
                                    0L);
 
        // Print after accumulator
        System.out.println("Current DoubleAccumulator"
                           + " value is: "
                           + num);
 
        // Update 2 in this instance
        // using accumulate() method
        num.accumulate(2);
 
        // Print after accumulator
        System.out.println("Updated DoubleAccumulator"
                           + " value is: "
                           + num);
    }
}

输出

Current DoubleAccumulator value is: 0.0
Updated DoubleAccumulator value is: 2.0

程序2

// Java program to demonstrate
// the accumulate() method
 
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create the DoubleAccumulator instance
        DoubleAccumulator num
            = new DoubleAccumulator(Double::sum,
                                    0L);
 
        // Print after accumulator
        System.out.println("Current DoubleAccumulator"
                           + " value is: "
                           + num);
 
        // Update 42.2 in this instance
        // using accumulate() method
        num.accumulate(42.2);
 
        // Print after accumulator
        System.out.println("Updated DoubleAccumulator"
                           + " value is: "
                           + num);
    }
}

输出

Current DoubleAccumulator value is: 0.0
Updated DoubleAccumulator value is: 42.2

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程