當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。