Get startedGet started for free

Data transformation with Streams

1. Data transformation with Streams

Previously, we explored `Streams` for filtering, iterating, and counting elements in a collection. While these methods help process data efficiently, they primarily focus on retrieving and analyzing information. Now, let’s take it a step further and explore data transformation with `Streams`.

2. Data transformation with streams

When working with data, we often need to transform formats, convert types, or aggregate results. Streams work with `List`, `Set`, `Map`, and arrays, allowing flexible data processing. We will use an `ArrayList` of names (`Alice`, `Bob`, `Charlie`, `David`) to explore transformation techniques.

3. Using Stream to transform elements

Let's look at an example of how we can use `Stream` to transform data. Before using these methods, we need to import `Set`, `Collectors`, and `Stream` from Java's built-in library. Since these classes are part of Java's standard library, no installation is required - just an import. In this example, we call `.stream()` on the `names` list we created earlier, storing the result in a `Stream` object. The data remains unchanged as of now.

4. Using Stream to transform elements (Continued)

Then we use `.map()` to convert each name to uppercase, modifying all elements in the `Stream`. Now we have a list of uppercase names. Then, `.collect(Collectors.toSet())` gathers the modified names into a new `Set`. It's important to note that `.collect()` requires a `Collector` to specify how the elements should be accumulated into a collection. Without a `Collector`, `.collect()` won't work because Java needs to know where to store the processed elements. Finally, we have a set with 4 elements: `CHARLIE`, `ALICE`, `BOB` and `DAVID`.

5. Using Stream for aggregation

The `.reduce()` method aggregates elements from a `Stream` into a single computed result. It is commonly used for summation, concatenation, and finding min/max values in a collection. The `.reduce()` method requires two inputs: Initial Value - This is the starting point for accumulation. `0` is commonly used for numerical summation. Binary Operator - A function that processes two values iteratively. `(x, y) -> x+y` is an example for summation. `x` stores the accumulated result. `y` represents the element being processed. The operation adds `y` to `x`, repeating until all elements have been processed.

6. Using Stream for aggregation: Example

Now, let's look at an example. First, we convert the `names` list into a `Stream` object. Then, we use `.map()` to transform each name into its character count. Finally, we apply `.reduce()` to add up all character counts. In this example, the first input for `.reduce()` is `0` because we are summing numbers and initializing the result to zero. The second input is a function that, in each iteration, adds the character count to the current sum, continuing until all elements are processed. At the end, we get a result of `20`, the total number of characters across all elements in the `names` list.

7. Let's practice!

It is time for you to practice!