The most pathetic way to implement HashMap in Mule 4

ANUPAM GOGOI
3 min readDec 8, 2023

Introduction

In this article, I am going to discuss how to implement a HashMap in Mule 4. Who has already worked with Mule3, it's well-known how to do it. But in Mule 4, I found it quite painful.

Mule 3

It's quite simple to declare a HashMap and add values to it in Mule 3.

#[new java.util.HashMap()]

That's all. You can use all the Get and Put methods of the Java HashMap.

Mule 4

I hated it but had to do it. For the TL;DR just copy and paste the below code.

<?xml version="1.0" encoding="UTF-8"?>

<mule
xmlns:outlook365="http://www.mulesoft.org/schema/mule/outlook365"
xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd
http://www.mulesoft.org/schema/mule/outlook365 http://www.mulesoft.org/schema/mule/outlook365/current/mule-outlook365.xsd">
<http:listener-config name="HTTP_Listener_config"
doc:name="HTTP Listener config"
doc:id="2b4cf156-0b38-4d7f-a016-318fd127ee59">
<http:listener-connection host="0.0.0.0"
port="8081" />
</http:listener-config>


<flow name="test-appFlow"
doc:id="5072b12e-f55e-48b6-87d4-0e0280e7f6e5">
<http:listener doc:name="Listener"
doc:id="9dde9763-a782-425d-81db-0276cf80dd5e"
config-ref="HTTP_Listener_config" path="/test" />


<set-variable value="#[{}]" doc:name="map"
doc:id="2e954952-221c-4a7c-8021-9ddebd612f44" variableName="map" />
<ee:transform doc:name="k1:value1"
doc:id="436d500d-a83f-443f-b179-f7d230aa9b7e">
<ee:message>
</ee:message>
<ee:variables>
<ee:set-variable variableName="map"><![CDATA[%dw 2.0
output application/java
---
vars.map ++ {"k1":["a","b"]}]]></ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform doc:name="k2:value2"
doc:id="487e17ca-64de-43a1-9b66-6603e6206658">
<ee:message>
</ee:message>
<ee:variables>
<ee:set-variable variableName="map"><![CDATA[%dw 2.0
output application/java
---
vars.map ++ {"k2":["c","d"]}]]></ee:set-variable>
</ee:variables>
</ee:transform>
<logger level="INFO" doc:name="Logger"
doc:id="13ab49c9-6b06-44a8-b291-b159c2fbd6c6" />
<set-variable value="k1" doc:name="Find the key"
doc:id="90204a71-46b1-4fab-9a5e-6332b6891f1a" variableName="key" />
<ee:transform doc:name="Transform Message" doc:id="b2a4cf67-0fa0-4e23-8735-dd0c00f9cfc6" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
vars.map[vars.key]]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="6a7738cd-fd73-4166-85cf-1754ae11be26" />
</flow>
</mule>

Below is the explanation of the code:

Step 1

Declare a variable (map in my case) with default value {}.

Step 2

Add a key value to the same variable declared in the previous step:

Step 3

Add another value.

Step 4

Set the name of the key to be searched in the map variable in a separate variable named key.

Step 5

Finally, get the value.

That's all.

Conclusion

I found it quite stupid way but it works. Let me know if you have a better solution.

--

--