当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 DoubleAccumulator accumulate() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。