Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use AtomicInteger getAndSet correctly?

I want to get the difference between consecutive list elements using Java stream and AtomicInteger:

List<Integer> list  = List.of(1,2,3,5,6,8,9,10,22,23,27);
AtomicInteger diff  = new AtomicInteger(1);


List<Integer> result = list.stream()
                           .skip(1)
                           .map(i -> diff.getAndSet(i - diff.get()))
                           .toList();

System.out.println(result);

Output is:

[1, 1, 2, 3, 3, 5, 4, 6, 16, 7]

I was expecting to get the difference of each element from previous like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[1, 1, 2, 1, 2, 1, 1, 12, 1, 4]

What am I doing wrong? How to get the desired output?

>Solution :

I wouldn’t use an atomic type for this. Instead I would start with a stream of the list indexes:

List<Integer> result = IntStream.range(0, list.size() - 1)
                       .map(i -> list.get(i + 1) - list.get(i))
                       .toList();

This (IMO) is simpler and cleaner (and more functional) than relying on side-effects.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading