Core Java

Constrast DataWeave and Java mapping operations

Main points:

  • DataWeave 2.0 provides mapping capabilities
  • Java and DataWeave can achieve the same mappings
  • DataWeave mapping operator is less verbose than Java

DataWeave map operator

The DataWeave 2.0 (Mule 4) map operator 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.

1
2
3
4
5
6
7
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 collector Collectors.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.

1
2
3
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.

1
2
3
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.

RESTful API design and RAML

There are five things to consider when designing a RESTful API. In this blog post I introduce those important aspects with examples to illustrate how to implement them in a API specification.

If you are designing a API using RAML you will want to include examples to ensure that the specification provides the high level of documentation that a modern API should. In this blog post I discuss four ways to specify examples in a RAML API specification.

Published on Java Code Geeks with permission by Alex Theedom, partner at our JCG program. See the original article here: Constrast DataWeave and Java mapping operations

Opinions expressed by Java Code Geeks contributors are their own.

Alex Theedom

Alex Theedom is a Senior Java Developer and has recently played a pivotal role in the architectural design and development of a microservice based, custom built lottery and instant win game platform. Alex has experience of Java web application development in a diverse range of fields including finance, e-learning, lottery and software development. He is the co-author of Professional Java EE Design Patterns and many articles.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button