jechaviz
jechaviz

Reputation: 551

Initialize a HashMap Variable in iReport

I want to create a variable $V{myMap} in iReport (3.7.3) and initialize a Map with this:

"key1":"value1","key2":"value2","key3:"value3" ...

Then in my report I will have a textField with this expression:

$V{myMap}.get("key1")

and i must to get

"value1"

What steps i must to follow to get that?


I read here:

How to use a java Hashmap as variable in JasperReport

these steps to set the variable properties:

set the following properties on the variable:

Name: myMap
Variable class: java.util.HashMap()
Reset type: None
Calculation: System
Variable expression: new java.util.HashMap()

But what about initialize it?

I know i can use groovy and use maps like in this article:

http://groovy.codehaus.org/Collections

But i dont' know which steps to follow.

Thanks in advance for share your knowledge!

Upvotes: 2

Views: 7423

Answers (3)

ARMAN 阿尔曼
ARMAN 阿尔曼

Reputation: 1334

If you need to create a singleton, immutable map, with a single entry, you can use Collections.singletonMap():

<variable name="hints" class="java.util.Map">
    <initialValueExpression><![CDATA[Collections.singletonMap("key", "value")]]></initialValueExpression>
</variable>

To get the value:

$V{hints}.get("key")

Upvotes: 1

jechaviz
jechaviz

Reputation: 551

This is the method to create the variable

<variable name="myMap" class="java.util.HashMap" resetType="None" calculation="System">
    <variableExpression><![CDATA[['key1':'value1', 'key2':'value2', 'key3':'value3']]]></variableExpression>
</variable>

Then for use it:

$V{myMap}.get("key1")

Will get

value1

Upvotes: 1

jjlema
jjlema

Reputation: 850

I would try using the property initial value expresion and trying to inicialize the hashmap using something like:

new HashMap<String , String>() {{
put(stringkey1, stringvalue1);
put(stringkey2, stringvalue2);
put(stringkey3, stringvalue3);
}};

Upvotes: -1

Related Questions