Java Stream partitioningBy() Tutorial 🎯

beginner
15 min

Java Stream partitioningBy() Tutorial 🎯

Welcome to the Java Stream partitioningBy() tutorial! In this lesson, we'll learn how to use the partitioningBy() method in Java Stream API. By the end of this tutorial, you'll understand the concept from scratch, and you'll be able to apply it in your projects. 📝 Let's get started!

Understanding Stream and partitioningBy() 💡

First, let's have a quick recap of what Stream is in Java. A Stream is a sequence of elements that can be processed in parallel or sequentially using various methods provided by the Stream API.

Now, the partitioningBy() method is a useful tool in Stream API that divides the elements of a Stream into two parts based on a given predicate. This method returns a PartitionedStream, which contains two Streams: a firstStream that contains elements for which the predicate returns true, and a secondStream containing elements for which the predicate returns false.

Creating a Java Program with partitioningBy() ✅

Let's write a simple Java program to demonstrate how to use the partitioningBy() method.

java
import java.util.stream.IntStream; public class Main { public static void main(String[] args) { IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9); IntStream oddStream = stream.filter(n -> n % 2 != 0); // filtering odd numbers IntStream evenStream = stream.filter(n -> n % 2 == 0); // filtering even numbers System.out.println("Odd Numbers:"); oddStream.forEach(System.out::println); System.out.println("\nEven Numbers:"); evenStream.forEach(System.out::println); } }

In the example above, we create an IntStream from an array of integers. Then, we use the filter() method to separate odd numbers and even numbers into two different streams (oddStream and evenStream). Finally, we print both streams to the console.

Quick Quiz
Question 1 of 1

In the above example, what is the purpose of the partitioningBy() method?

Using partitioningBy() Method 💡

Now that we understand the basics, let's use the partitioningBy() method in our program.

java
import java.util.stream.IntStream; public class Main { public static void main(String[] args) { IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionedIntConsumer<Integer> partitioner = (t1, t2) -> { System.out.println("Odd Numbers:"); t1.forEach(System.out::println); System.out.println("\nEven Numbers:"); t2.forEach(System.out::println); }; IntStream oddStream = stream.partitionedBy(n -> n % 2 != 0); oddStream.forEachOrdered(partitioner); } }

In this example, we use the partitioningBy() method to create a PartitionedIntConsumer<Integer> called partitioner. The partitioner takes two streams as arguments: the firstStream and the secondStream. We use this partitioner with the partitionedBy() method to separate odd and even numbers into the firstStream and the secondStream, respectively. Finally, we print both streams using the forEachOrdered() method and the partitioner.

Key Takeaways 📝

  • The partitioningBy() method in Java Stream API divides the elements of a Stream into two parts based on a given predicate
  • The partitionedBy() method returns a PartitionedStream, which contains two Streams: a firstStream and a secondStream
  • The partitionedBy() method can be used to separate the elements of a Stream based on a condition, making it easier to work with large datasets in a more organized way.

Quiz 💡

Quick Quiz
Question 1 of 1

In the following code snippet, what does the partitioner variable represent?