Alexander Kazantsev
Alexander Kazantsev

Reputation: 340

Print whole HashMap by jasperreport

I need to print whole map by jasperreport

Example:

Column1 Column2

key1        val1

key2        val2

key2        val2

What is data source should be choosen?

What should containts report source?

PS: I don't want to get values directly like $P{key11}

Upvotes: 4

Views: 3798

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

I would try something like this

Map<String,Object> map = new HashMap<String,Object>();
//your map values....
Set<Entry<String,Object>> set = map.entrySet();
JRBeanCollectionDataSource bds = new JRBeanCollectionDataSource(set);

in the jasper report your fields will be

<field name="key" class="java.lang.String"/>
<field name="value" class="java.lang.Object"/>

Note: How I'm using the Map.Entry bean getKey() and getValue() to created my datasource

The values will not be sorted if you like to sort them just implement a Comparator and use Collections.

    List<Entry<String, Object>> list = new ArrayList<Entry<String, Object>>();
    list.addAll(set);
    Collections.sort(list, new Comparator<Entry<String, Object>>() {
        @Override
        public int compare(Entry<String, Object> o1, Entry<String, Object> o2) {
            // TODO Implement you sorting
            return 0;
        }
    });
    JRBeanCollectionDataSource bdsSorted = new JRBeanCollectionDataSource(list);

Upvotes: 2

Related Questions