In this post, we will see how to double the even and odd numbers present in an ArrayList separately with the help of Java 8 Streams.
Let’s say: There is an ArrayList of 10 integer values as shown below. In the first example, using Streams, we are going to double each even integer of the ArrayList and In the second example, we are going to double odd integers.
1 | List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); |
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Double of Even Numbers using Streams * @author Deepak Verma */ import static java.util.stream.Collectors.toList; import java.util.Arrays; import java.util.List; public class DoubleOfEvenUsingStreams { public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); List<Integer> doubleValueListOfEvenIntegers = list.stream() .filter(i -> i%2 == 0) .map(i -> i*2) .collect(toList()); System.out.println("Double values of Even ArrayList Integers: "+doubleValueListOfEvenIntegers); } } |
Output:
1 | Double values of Even ArrayList Integers: [4, 8, 12, 16, 20] |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Double of Odd Numbers using Streams * @author Deepak Verma */ import static java.util.stream.Collectors.toList; import java.util.Arrays; import java.util.List; public class DoubleOfOddUsingStreams { public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); List<Integer> doubleValueListOfOddIntegers = list.stream() .filter(i -> i%2 != 0) .map(i -> i*2) .collect(toList()); System.out.println("Double values of Odd ArrayList Integers: "+doubleValueListOfOddIntegers); } } |
Output:
1 | Double values of Odd ArrayList Integers: [2, 6, 10, 14, 18] |
That’s it. It’s that easy to double the even and odd numbers of ArrayList with the power of Java 8 Streams.
If you like this post, please check out my other similar posts:
How to add/sum up the ArrayList integers using Java8 Streams
Hi, your filter function works only because of List input and ONLY because it has odd and even numbers. If you place there random even numbers, for example 22, 33, 44, 24, 56 – it won’t work it will do pow(x, 2) to all numbers, not only to even. Basically, our code incorrect in terms of what it should do
Hi Bohdan,
As what I understood, you said if you tried with list items [22, 33, 44, 24, 56], it’s not working as intended.
Well, I tried, it’s working completely fine.
Please correct me if I didn’t understand your question clearly. Kindly, let me know and i’ll try to help out asap.
Thanks
If you are interested in work in nursing, many online college classes for
nursing are available. This makes the students all-rounder and they
might be for sure that they can shall click virtually any tough
interview on the first attempt itself. Running genuine software
that you’ve legitimately purchased helps protect you
from these dangers and provides additional benefits,
including support to troubleshoot a problem when you’re printing out
an assignment with the eleventh hour and regular updates to help
you be sure that your computer and the facts about it are safe from system crashes or prying eyes.
Hello There. I found your blog using msn. This is an extremely well written article.
I’ll make sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I’ll definitely comeback.
Thank you. I am glad that my content helped you.
Thanks
Hey,
lately I have finished preparing my ultimate tutorial:
+++ [Beginner’s Guide] How To Make A Website From Scratch +++
I would really apprecaite your feedback, so I can improve my craft.
Link: https://janzac.com/how-to-make-a-website/
If you know someone who may benefit from reading it, I would be really grateful for sharing a link.
Much love from Poland!
Cheers
Hello Jan,
Honestly, this is a wonderful website as you have accumulated almost everything that is required to build a website from scratch.
I am sure gonna use it for myself as well. It’s great stuff.
Let me share with one of friend too who are looking to start a website and your content could be helpful to him.
Cheers