AtomicReferenceArray getAndUpdate() method in Java with Examples Last Updated : 16 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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 operation specified in the object to the current value. It returns the previous value.Syntax: public final E getAndUpdate(int i, UnaryOperator<E> updateFunction) Parameters: This method accepts: index i to update the index value andupdateFunction which is a side-effect-free function. Return value: This method returns the previous value.Below programs illustrate the getAndUpdate() method: Program 1: Java // Java program to demonstrate // AtomicReferenceArray.getAndUpdate() method import java.util.concurrent.atomic.*; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // an array String a[] = { "GFG", "JS", "PYTHON", "JAVA" }; // AtomicReferenceArray with array AtomicReferenceArray<String> array = new AtomicReferenceArray<>(a); // Print AtomicReferenceArray System.out.println( "The AtomicReferenceArray before update : " + array); // Index int index = 2; // Declaring the accumulatorFunction // applying function UnaryOperator add = (u) -> u.toString() + " and ML"; // apply getAndUpdate() String value = array.getAndUpdate(index, add); // print AtomicReferenceArray System.out.println("previous value of index 2:" + value); System.out.println("The AtomicReferenceArray " + "after update: " + array); } } Output: Program 2: Java // Java program to demonstrate // AtomicReferenceArray.getAndUpdate() method import java.util.concurrent.atomic.*; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // an array Integer a[] = { 213, 1234, 4543, 345 }; // AtomicReferenceArray with array AtomicReferenceArray<Integer> array = new AtomicReferenceArray(a); // Print AtomicReferenceArray System.out.println( "The AtomicReferenceArray" + " before update : " + array); // Index int index = 2; // Declaring the accumulatorFunction // applying function UnaryOperator add = (u) -> Integer.parseInt(u.toString()) * 200; // apply getAndUpdate() int value = array.getAndUpdate(index, add); // print AtomicReferenceArray System.out.println("previous value of index 2:" + value); System.out.println("updated value of index 2:" + array.get(2)); System.out.println("The AtomicReferenceArray " + "after update: " + array); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#getAndUpdate(java.util.function.UnaryOperator) Comment More infoAdvertise with us Next Article AtomicReferenceArray getAndUpdate() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-util-concurrent-atomic package Java-AtomicReferenceArray Practice Tags : Java Similar Reads AtomicReference getAndUpdate() method in Java with Examples The getAndUpdate() method of a AtomicReference class is used to atomically updates which updates the current value of the AtomicReference by applying the specified updateFunction operation on the current value. It takes an object of updateFunction interface as its parameter and applies the operation 2 min read AtomicReferenceArray getAndSet() method in Java with Examples The getAndSet() method of a AtomicReferenceArray class is used to atomically sets the value of index i of AtomicReferenceArray object to newValue which is passed as parameter and returns the old value of the AtomicReferenceArray object, with memory effects as specified by VarHandle.getAndSet(java.la 2 min read AtomicReferenceArray getAndAccumulate() method in Java with Examples The getAndAccumulate() method of a AtomicReferenceArray class is used to atomically updates the element at index i of AtomicReferenceArray with the results of applying the given accumulatorFunction to the current and given values and returns the previous value. The accumulatorFunction should be side 2 min read AtomicReferenceArray getOpaque() method in Java with Examples The getOpaque() method of a AtomicReferenceArray class is used to return the value of the element at index i for this AtomicReferenceArray object with memory effects as specified by VarHandle.getOpaque(java.lang.Object...). This VarHandle.getOpaque(java.lang.Object...) method handles operation with 2 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 Like