Contrast DataWeave and Java Mapping Operations
Java and DataWeave provided mapping capabilities and can achieve the same results, however the DataWeave mapping function is less verbose than Java.
Join the DZone community and get the full member experience.
Join For FreeMain Points
- DataWeave 2.0 provides mapping capabilities.
- Java and DataWeave can achieve the same mappings.
- DataWeave mapping function is less verbose than Java.
DataWeave Map Function
The DataWeave 2.0 (Mule 4) map function shares similarities with the map()
method from Java's Stream
class.
Mapping is a Transformative Operation
The idea of mapping is to transform each element of an array and output a new array of transformed elements. An expression is provided that performs the transformation. It is applied to each element in the array and collected into another new array.
Apply a Mapping to an Array in Java
In Java, a transformative expression is applied by passing it to the map()
method of the Stream
class. It is applied in turn to each element of the array and collected to a new List
. In the following code snippet, the inline array is transformed into a stream so that mapping can be performed.
List<String> pets = Arrays.asList(
new String[] { "cat", "dog", "fish" }
);
List<String> newPets = pets.stream()
.map(e -> e.toUpperCase())
.collect(Collectors.toList());
The transformation is performed by the lambda expression, e -> e.toUpperCase()
where the variable e
represents each element in the array. The result of the transformation is added to a new List
using a collectorCollectors.toList()
.
There is a 'short cut' expression that you can use in place of the explicit lambda expression. It is String::toUpperCase
the above code would now look as follows:
pets.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
Apply a Mapping to an Array in DataWeave
In DataWeave, a transformative expression is applied to each element of an array and outputted to a new array containing these new transformed elements.
var pets = ["cat", "dog", "fish"]
---
pets map upper($)
The upper()
function is applied to each element in the pets
array and transformed. Each transformed element is put into a new array. This new array is the output of this operation. The dollar ($) symbol represents each element in the array as the map
function iterates over the array. The upper()
function is a lambda function from the dw::Core
module. It is automatically imported into all DataWeave scripts.
Final Thoughts
DataWeave has been designed to transform data and does so in a performant way. The code is concise and easy to understand. As you can see, Java is more verbose but provides much more capabilities than data transformation. Consider the mapObject DataWeave function that maps an object to an object and the pluck DataWeave function, which can be used to deconstruct an object by its index, keys, and values.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments