# AtomicLongArray getAndUpdate() method in Java with Examples

• Last Updated : 12 Feb, 2019

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 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 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]
```

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5922004,"math_prob":0.8107017,"size":3691,"snap":"2021-43-2021-49","text_gpt3_token_len":882,"char_repetition_ratio":0.1787361,"word_repetition_ratio":0.55813956,"special_character_ratio":0.24492007,"punctuation_ratio":0.17565486,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95595366,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-08T08:10:43Z\",\"WARC-Record-ID\":\"<urn:uuid:04edb0e6-546a-40f5-973e-df907f3db651>\",\"Content-Length\":\"127974\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d90fd7fe-8838-47a8-bb9b-11e0aed59daf>\",\"WARC-Concurrent-To\":\"<urn:uuid:010f6029-a3a0-4ae6-b229-5878f48b00de>\",\"WARC-IP-Address\":\"204.237.142.128\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/atomiclongarray-getandupdate-method-in-java-with-examples/?ref=leftbar-rightbar\",\"WARC-Payload-Digest\":\"sha1:2UMWJVNA5OUZVHRSW37NCPAQRH3F5WZ2\",\"WARC-Block-Digest\":\"sha1:ODV7QIG7AJW3YD5IX7NEDWKSPSLWMKPJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363445.41_warc_CC-MAIN-20211208053135-20211208083135-00208.warc.gz\"}"}