Java ProgramsArraysSeparate Even and Odd

Separate Even and Odd in Java

beginner·  Arrays  ·  Array

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.

Input
[3, 8, 15, 22, 7, 14, 9]
Output
Evens: [8, 22, 14] Odds: [3, 15, 7, 9]

Java Program

Java
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

Evens: [8, 22, 14] Odds: [3, 15, 7, 9]

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.

How It Works
  1. 1Two empty lists, evens and odds, are created to hold each group.
  2. 2A for-each loop visits every element of the array.
  3. 3num % 2 == 0 checks whether the number is even; a match appends it to evens, otherwise it's appended to odds.
  4. 4By the end of the loop, every original element has landed in exactly one of the two lists, in its original relative order.
For [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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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

ArrayListmodulo operatorfor-each loop

Approach 2: Java 8

Java
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

Evens: [8, 22, 14] Odds: [3, 15, 7, 9]

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.

How It Works
  1. 1Arrays.stream(arr).boxed() converts the int[] into a Stream<Integer>, since collectors work with objects, not primitives.
  2. 2Collectors.partitioningBy(n -> n % 2 == 0) splits the stream into a Map<Boolean, List<Integer>>, using the same even/odd test as the loop version.
  3. 3partitioned.get(true) retrieves the list of elements that passed the test — the evens.
  4. 4partitioned.get(false) retrieves everything that failed it — the odds.
Partitioning [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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: partitioningBy() still visits every element once while building the two lists behind a single boolean key.

Key Concepts

StreamCollectors.partitioningBy()

Related Programs