|
| 1 | +--- |
| 2 | +id: refactoring.iteration.withtransformation |
| 3 | +title: Converting Iteration with transformation |
| 4 | +slug: learn/refactoring-to-functional-style/iteartionwithtransformation |
| 5 | +type: tutorial-group |
| 6 | +group: refactoring-to-functional-style |
| 7 | +layout: learn/tutorial-group.html |
| 8 | +subheader_select: tutorials |
| 9 | +main_css_id: learn |
| 10 | +toc: |
| 11 | +- Transforming while Iterating {transforming} |
| 12 | +- From Imperative to Functional Style {imperativetofunctional} |
| 13 | +- Picking Elements to Transform {picking} |
| 14 | +- Mappings {mappings} |
| 15 | +description: "Converting Imperative Iteration with transformation to Functional Style." |
| 16 | +last_update: 2024-01-08 |
| 17 | +author: ["VenkatSubramaniam"] |
| 18 | +--- |
| 19 | + |
| 20 | +<a id="transforming"> </a> |
| 21 | +## Transforming while Iterating |
| 22 | + |
| 23 | +In the previous articles in this [tutorial series](id:refactoring) we looked at converting loops with `if` or conditional statements in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration that transforms data to the functional style. In addition, we'll also refactor code that mixes transforming data with code that picks select elements before the transformation. |
| 24 | + |
| 25 | +Anytime we are transforming data in an imperative style loop, we can use the `map()` function in the functional style. Let's see how. |
| 26 | + |
| 27 | + |
| 28 | +<a id="imperativetofunctional"> </a> |
| 29 | +## From Imperative to Functional Style |
| 30 | + |
| 31 | +Here's an example of an iteration, using the `foreach`, that transforms to uppercase a collection of names: |
| 32 | + |
| 33 | +```java |
| 34 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 35 | + |
| 36 | +for(String name: names) { |
| 37 | + System.out.println(name.toUpperCase()); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Each step through the iteration, the `name` variable is bound to a new value. As the iteration advances from one element to the next in the given collection, each name is transformed to uppercase using the `toUpperCase()` function and the resulting value is printed. We have already seen, in the previous article, how to convert the imperative style `foreach` to the functional style—using the `stream()` internal iteration. If we merely apply what we have seen before, the resulting functional style code would be rather unwieldy, with the lambda passed to `forEach` performing both transformation and printing, like so: |
| 42 | + |
| 43 | +```java |
| 44 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 45 | + |
| 46 | +names.forEach(name -> System.out.println(name.toUpperCase())); //Don't do this |
| 47 | +``` |
| 48 | + |
| 49 | +Even though the above functional style code will produce the same results as the imperative style code, the lambda passed to the `forEach()` function is not cohesive, hard to read, and hard to change. |
| 50 | + |
| 51 | +Before refactoring the imperative style code above to the function style, we should first refactor the imperative style to yet another imperative style implementation to make each line more cohesive, like so: |
| 52 | + |
| 53 | +```java |
| 54 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 55 | + |
| 56 | +for(String name: names) { |
| 57 | + String nameInUpperCase = name.toUpperCase(); |
| 58 | + System.out.println(nameInUpperCase); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +From the previous articles in this series, we know that the `for` can turn into a `stream()` and the printing of the value can be done from within the `forEach()`. That leaves us with the transformation, the call to the `toUpperCase()` function. Such transformations can be done using the `map()` operation on the `stream()`. |
| 63 | + |
| 64 | +```java |
| 65 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 66 | + |
| 67 | +names.stream() |
| 68 | + .map(name -> name.toUpperCase()) |
| 69 | + .forEach(nameInUpperCase -> System.out.println(nameInUpperCase)); |
| 70 | +``` |
| 71 | +The `map()` operation transforms the data to a different value based on the function invoked from within the lambda expression that's passed to `map()`. In this example, each name is transformed to its uppercase value. The transformed value is then printed out from within the lambda expression passed to `forEach()`. |
| 72 | + |
| 73 | +We can make the code a bit more concise by using method references instead of lambda expressions, like so: |
| 74 | + |
| 75 | +```java |
| 76 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 77 | + |
| 78 | +names.stream() |
| 79 | + .map(String::toUpperCase) |
| 80 | + .forEach(System.out::println); |
| 81 | +``` |
| 82 | + |
| 83 | +Use the `map()` function to transform data while iterating over a collection of data. |
| 84 | + |
| 85 | +<a id="picking"> </a> |
| 86 | +## Picking Elements to Transform |
| 87 | + |
| 88 | +Suppose, in the middle of the iteration, we want to pick some values from the collection based on some condition and apply a transformation only on those elements. For example, what if we want to transform and print only names of length 4? In the imperative style we could do the following: |
| 89 | + |
| 90 | +```java |
| 91 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 92 | + |
| 93 | +for(String name: names) { |
| 94 | + if(name.length() == 4) { |
| 95 | + System.out.println(name.toUpperCase()); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +We already know that the imperative style `if` can be refactored to the `filter()` function in the functional style. We can perform the transformation, using `map()`, after the `filter()` operation, like so: |
| 101 | + |
| 102 | +```java |
| 103 | +List<String> names = List.of("Jack", "Paula", "Kate", "Peter"); |
| 104 | + |
| 105 | +names.stream() |
| 106 | + .filter(name -> name.length() == 4) |
| 107 | + .map(String::toUpperCase) |
| 108 | + .forEach(System.out::println); |
| 109 | +``` |
| 110 | + |
| 111 | +The `filter()` function discards data that's not desired and passes on only the values we like to use. The `map()` function transforms the values it sees after the filter. |
| 112 | + |
| 113 | +<a id="mappings"> </a> |
| 114 | +## Mappings |
| 115 | + |
| 116 | +Anywhere you see transformation of data within a for loop, use the `map()` function to perform the transformation in the functional style. In addition, if the body of the loop has a `if` statement to selectively some value for transformation, then use the `stream()` API with the call to the `filter()` method before using the `map()` method. |
0 commit comments