Friday, September 26, 2025
HomeLanguagesJavaAtomicIntegerArray getAndUpdate() method in Java with Examples

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 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 int getAndUpdate(int i, IntUnaryOperator 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 Integer value which is the value after applying the specified update function.

Below programs illustrate the above method:
Program 1:




// Java program that demonstrates
// the getAndUpdate() 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 squaredFunction = (l) -> l * l;
  
        // Updating the value at idx
        // applying updateFunction and
        // storing the previous value
        int prev = arr.getAndUpdate(idx, squaredFunction);
  
        // Displaying the previous value
        System.out.println("The value before update"
                           + " at index " + idx
                           + " is " + prev);
  
        // Displaying the AtomicIntegerArray
        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 program that demonstrates
// the getAndUpdate() 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 and
        // storing the previous value
        int prev = arr.getAndUpdate(idx, cubeFunction);
  
        // Displaying the previous value
        System.out.println("The value before update"
                           + " at index " + idx
                           + " is " + prev);
  
        // Displaying the AtomicIntegerArray
        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/AtomicIntegerArray.html#getAndUpdate-int-java.util.function.IntUnaryOperator-

RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6756 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS