1. Introduction
In this article, I will discuss how we can use Java 8 methods to count and add even odd numbers of an array. It will be fun to use this new method instead of plain old for-loops.
2. Content
We will see 2 examples of:
- How to count even and odds numbers in an array
- Add all even numbers and odd numbers
3. Count even and odd numbers using for loop
This is simple to do. Just iterate through the array and check if the number is even or odd and increment the respective count, i.e. for even numbers increment evenCount else increment oddCount.
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int evenCount = 0; int oddCount = 0; for (int elem : a) { if (elem % 2 == 0) { evenCount++; } else { oddCount++; } } Map<Boolean, Integer> result = new HashMap<>(); result.put(Boolean.TRUE, evenCount); result.put(Boolean.FALSE, oddCount); // Test Assert.assertEquals(Integer.valueOf(4), result.get(Boolean.TRUE)); Assert.assertEquals(Integer.valueOf(5), result.get(Boolean.FALSE));
4. Using Collectors.partitioningBy and Collectors.counting.
Now let us make use of Stream and Collector methods. partitioningBy method of Collectors class takes 2 arguments, Predicate and Collector. Predicate is used to partition the elements based on the condition and they pass those elements to the Collector for further processing.
So the Predicate will test if the number is even or not. The Collector will get all even numbers, count them(Collectors.counting()) and save it in Map’s value where the key is Boolean.True. Reverse is true for all numbers that are not even.
import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.partitioningBy; int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Map<Boolean, Long> result = Arrays .stream(a) // IntStream .boxed() // Stream<Integer> .collect( Collectors.partitioningBy( // Partition based on Predicate val -> val.intValue() % 2 == 0, // Count numbers Collectors.counting())); // Test Assert.assertEquals(Long.valueOf(4), result.get(Boolean.TRUE)); Assert.assertEquals(Long.valueOf(5), result.get(Boolean.FALSE));
5. Add all even and odd numbers using Collectors.partitioningBy and Collectors.summingInt.
In this example, the Predicate will test if the number is even or not. If the number is even, we will add them all and save in Map’s value where the key is as Boolean.TRUE. All other numbers that are not even we will add them too but save them in Map’s value where the key is Boolean.FALSE.
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Map<Boolean, Integer> result = Arrays .stream(a) // IntStream .boxed() // Stream<Integer> .collect( Collectors.partitioningBy( // Partition based on Predicate val -> val.intValue() % 2 == 0, // add values partitioned by Predicate Collectors.summingInt(Integer::intValue))); //Â Test Assert.assertEquals(Integer.valueOf(20), result.get(Boolean.TRUE)); Assert.assertEquals(Integer.valueOf(25), result.get(Boolean.FALSE));
6. Conclusion
partitioningBy is a powerful method through which we can partition the data based on Predicate. It also allows us to process data once it is partitioned using any Collector.