AtomicIntegerArray updateAndGet() method in Java with Examples Last Updated : 27 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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. Syntax: public final int updateAndGet(int i, IntegerUnaryOperator 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 int 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 updateAndGet() function import java.util.concurrent.atomic.AtomicIntegerArray; import java.util.function.IntUnaryOperator; public class GFG { public static void main(String args[]) { // Initializing an array int a[] = { 1, 2, 3, 4, 5 }; // Initializing an AtomicIntegerArray with array a AtomicIntegerArray arr = new AtomicIntegerArray(a); // Displaying the AtomicIntegerArray System.out.println("The array : " + arr); // Index where update is to be made int idx = 4; // Declaring the updateFunction IntUnaryOperator squaredFunction = (l) -> l * l; // Updating the value at idx // applying updateFunction arr.updateAndGet(idx, squaredFunction); // Displaying the AtomicIntegerArray System.out.println("The array after update : " + arr); } } Output: The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 4, 25] Program 2: Java // Java program that demonstrates // the updateAndGet() function import java.util.concurrent.atomic.AtomicIntegerArray; import java.util.function.IntUnaryOperator; public class GFG { public static void main(String args[]) { // Initializing an array int a[] = { 1, 2, 3, 4, 5 }; // Initializing an AtomicIntegerArray with array a AtomicIntegerArray arr = new AtomicIntegerArray(a); // Displaying the AtomicIntegerArray System.out.println("The array : " + arr); // Index where update is to be made int idx = 3; // Declaring the updateFunction IntUnaryOperator cubeFunction = (l) -> l * l * l; // Updating the value at idx // applying updateFunction arr.updateAndGet(idx, cubeFunction); // Displaying the AtomicIntegerArray System.out.println("The array after update : " + arr); } } Output: The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 64, 5] Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#updateAndGet-int-java.util.function.IntUnaryOperator- Comment More infoAdvertise with us Next Article AtomicLong updateAndGet() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-concurrent-package Java-AtomicIntegerArray Practice Tags : Java Similar Reads AtomicInteger updateAndGet() method in Java with Examples The Java.AtomicInteger.updateAndGet() 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 IntUnaryOperator interface as its parameter and applies the operation specified in the object to the curren 1 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 updateAndGet() method in Java with Examples 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 2 min read AtomicIntegerArray set() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.set() is an inbuilt method in Java that sets a given value at any position of the AtomicIntegerArray. This method takes the index value of the AtomicIntegerArray as parameter and updates the value at that index. This method does not return any value 2 min read AtomicLong updateAndGet() method in Java with Examples The Java.AtomicLong.updateAndGet() 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 toString() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.toString() is an inbuilt method in Java that returns a string that textually represents the current values of the AtomicIntegerArray. The resulting string is a concise and informative representation that is easy for a person to read. It is used to e 2 min read Like