AtomicLongArray getAndUpdate() method in Java with Examples Last Updated : 12 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The Java.util.concurrent.atomic.AtomicLongArray.getAndUpdate() is an inbuilt method in Java that updates the value at any given index of the AtomicLongArray after applying a given update function on the value at that index. The method takes the index value of the AtomicLongArray and the update function as the parameters and updates the value at that index by applying the update function on that value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The only difference between the getAndUpdate() and updateAndGet() function is that the former returns the value before the update and the latter returns the value after the update. Syntax: public final long getAndUpdate(int i, LongUnaryOperator updateFunction) Parameters: The function accepts two parameters: i which is the index where update is to be made. updateFunction which is the update function of single argument that tells what update is to be made. Return value: The function returns a long value which is the value after applying the specified update function. Below programs illustrate the above method: Program 1: Java // Java program that demonstrates // the getAndUpdate() function import java.util.concurrent.atomic.AtomicLongArray; import java.util.function.LongUnaryOperator; public class GFG { public static void main(String args[]) { // Initializing an array long a[] = { 1, 2, 3, 4, 5 }; // Initializing an AtomicLongArray with array a AtomicLongArray arr = new AtomicLongArray(a); // Displaying the AtomicLongArray System.out.println("The array : " + arr); // Index where update is to be made int idx = 3; // Declaring the updateFunction LongUnaryOperator squaredFunction = (l) -> l * l; // Updating the value at idx // applying updateFunction and // storing the previous value long prev = arr.getAndUpdate(idx, squaredFunction); // Displaying the previous value System.out.println("The value before update" + " at index " + idx + " is " + prev); // Displaying the AtomicLongArray System.out.println("The array after update : " + arr); } } Output: The array : [1, 2, 3, 4, 5] The value before update at index 3 is 4 The array after update : [1, 2, 3, 16, 5] Program 2: Java // Java program that demonstrates // the getAndUpdate() function import java.util.concurrent.atomic.AtomicLongArray; import java.util.function.LongUnaryOperator; public class GFG { public static void main(String args[]) { // Initializing an array long a[] = { 1, 2, 3, 4, 5 }; // Initializing an AtomicLongArray with array a AtomicLongArray arr = new AtomicLongArray(a); // Displaying the AtomicLongArray System.out.println("The array : " + arr); // Index where update is to be made int idx = 3; // Declaring the updateFunction LongUnaryOperator cubeFunction = (l) -> l * l * l; // Updating the value at idx // applying updateFunction and // storing the previous value long prev = arr.getAndUpdate(idx, cubeFunction); // Displaying the previous value System.out.println("The value before update" + " at index " + idx + " is " + prev); // Displaying the AtomicLongArray System.out.println("The array after update : " + arr); } } Output: The array : [1, 2, 3, 4, 5] The value before update at index 3 is 4 The array after update : [1, 2, 3, 64, 5] Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#getAndUpdate-int-java.util.function.LongUnaryOperator- Comment More infoAdvertise with us Next Article AtomicLongArray getAndAdd() method in Java with Examples R rupesh_rao Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicLongArray Practice Tags : Java Similar Reads AtomicLong getAndUpdate() method in Java with Examples The Java.AtomicLong.getAndUpdate() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of LongUnaryOperator interface as its parameter and applies the operation specified in the object to the current 1 min read AtomicIntegerArray getAndUpdate() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.getAndUpdate() is an inbuilt method in Java that updates the value at any given index of the AtomicIntegerArray after applying a given update function on the value at that index. The method takes the index value of the AtomicIntegerArray and the upd 3 min read AtomicLongArray getAndSet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.getAndSet() is an inbuilt method in Java that atomically sets a given value at any given position of the AtomicLongArray. The method takes the index value of the AtomicLongArray and the value to set as the parameters. It returns the value at the given 3 min read AtomicLongArray getAndAdd() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.getAndAdd() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicLongArray. This method takes the index value of the AtomicLongArray and the value to be added as the parameters and returns the value 3 min read AtomicLongArray getAndAccumulate() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.getAndAccumulate() is an inbuilt method in java that atomically updates the value at any index of the AtomicLongArray with the result after applying the given function to the current and given values, returning the previous value. This method accepts t 3 min read AtomicReferenceArray getAndUpdate() method in Java with Examples The getAndUpdate() method of a AtomicReferenceArray class is used to atomically updates which updates the current value of the AtomicReferenceArray by applying the specified updateFunction operation on the current value. It takes an object of updateFunction interface as its parameter and applies the 2 min read Like