Separate Even and Odd in Java
Problem
Separating even and odd numbers means grouping an array's elements into two lists based on divisibility by 2, while keeping each group in its original relative order.
Given an array of integers, print its even numbers and odd numbers as two separate groups.
Java Program
import java.util.ArrayList;
import java.util.List;
public class SeparateEvenOdd {
public static void main(String[] args) {
int[] arr = {3, 8, 15, 22, 7, 14, 9};
List<Integer> evens = new ArrayList<>();
List<Integer> odds = new ArrayList<>();
for (int num : arr) {
if (num % 2 == 0) { // no remainder means even
evens.add(num);
} else {
odds.add(num);
}
}
System.out.println("Evens: " + evens);
System.out.println("Odds: " + odds);
}
}Output
Core Logic
Checking each number's remainder when divided by 2, and appending it to one of two lists, sorts every element into an even group or an odd group in a single pass.
- 1Two empty lists,
evensandodds, are created to hold each group. - 2A for-each loop visits every element of the array.
- 3
num % 2 == 0checks whether the number is even; a match appends it toevens, otherwise it's appended toodds. - 4By the end of the loop, every original element has landed in exactly one of the two lists, in its original relative order.
[3, 8, 15, 22, 7, 14, 9], 8, 22, and 14 land in evens, while 3, 15, 7, and 9 land in odds.Key Point: Unlike moving elements around within the original array, this builds two brand-new lists — the source array itself is left untouched.
Why: Each element is visited once and appended to one of two lists, whose combined size always equals the original array's length.
Key Concepts
Approach 2: Java 8
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SeparateEvenOddStream {
public static void main(String[] args) {
int[] arr = {3, 8, 15, 22, 7, 14, 9};
// Splits the stream into two groups keyed by true (even) and false (odd)
Map<Boolean, List<Integer>> partitioned = Arrays.stream(arr)
.boxed()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("Evens: " + partitioned.get(true));
System.out.println("Odds: " + partitioned.get(false));
}
}
Output
Core Logic
Collectors.partitioningBy() already knows how to split a stream into two groups based on a true/false test — one collector call replaces the whole if/else loop.
- 1
Arrays.stream(arr).boxed()converts theint[]into aStream<Integer>, since collectors work with objects, not primitives. - 2
Collectors.partitioningBy(n -> n % 2 == 0)splits the stream into aMap<Boolean, List<Integer>>, using the same even/odd test as the loop version. - 3
partitioned.get(true)retrieves the list of elements that passed the test — the evens. - 4
partitioned.get(false)retrieves everything that failed it — the odds.
[3, 8, 15, 22, 7, 14, 9] produces the same two groups as the manual version, keyed by true for evens and false for odds.Key Point: partitioningBy() always returns a map with exactly two keys, true and false — even if one group turns out empty, unlike groupingBy(), which would simply omit a key with no matches.
Why: partitioningBy() still visits every element once while building the two lists behind a single boolean key.