DoubleAccumulator accumulate() method in Java with Examples

The Java.DoubleAccumulator.accumulate() method is an inbuilt method in Java that updates with the given value in this DoubleAccumulator instance. It means that it takes a double value as a parameter and adds it up to this instance of DoubleAccumulator on which it is called.

Syntax:

public void accumulate(double valueToBeAccumulated)

Parameters: This method accepts a single mandatory parameter valueToBeAccumulated which is the double value to be updaated in the current instance of this DoubleAccumulator.



Return value: This method do not return any value. It just updates this DoubleAccumulator.

Program below demonstrates the function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.