AtomicReferenceArray updateAndGet() method in Java with Examples Last Updated : 13 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The updateAndGet() 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 V updateAndGet( UnaryOperator<V> 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 updateAndGet() method: Program 1: Java // Java program to demonstrate // AtomicReferenceArray.updateAndGet() 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 updateAndGet() String value = array.updateAndGet(index, add); // print AtomicReferenceArray System.out.println("updated value of index 2:" + value); System.out.println( "The AtomicReferenceArray after update: " + array); } } Output: Program 2: Java // Java program to demonstrate // AtomicReferenceArray.updateAndGet() 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()) * 500; // apply updateAndGet() int value = array.updateAndGet(index, add); // print AtomicReferenceArray System.out.println("updated value of index 2:" + value); System.out.println( "The AtomicReferenceArray after update: " + array); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#updateAndGet(java.util.function.UnaryOperator) Comment More infoAdvertise with us Next Article AtomicIntegerArray updateAndGet() 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 updateAndGet() method in Java with Examples The updateAndGet() 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 AtomicIntegerArray updateAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.updateAndGet() 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 updateAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.updateAndGet() 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 funct 3 min read AtomicReferenceArray weakCompareAndSet() method in Java with Examples The weakCompareAndSet() method of a AtomicReferenceArray class is used to atomically sets the value of the element at index i to newValue to newValue for AtomicReferenceArray if the current value is equal to expectedValue passed as parameter. This method updates the value at index i with memory sema 2 min read AtomicReferenceArray set() method in Java with Examples The set() method of a AtomicReferenceArray class is used to set the value of the element at index i to newValue. Both index i and newValue are passed as parameters to the method. This method set the value with memory semantics of reading as if the variable was declared volatile type of variable. Syn 2 min read AtomicReferenceArray setRelease() method in Java with Examples The setRelease() method of a AtomicReferenceArray class is used to set the value of the element at index i to newValue. Both index i and newValue are passed as parameters to the method. This method set the value with memory effects as specified by VarHandle.setRelease(java.lang.Object...). This meth 2 min read Like