How do I read mapped bean’s property value?

In this example you’ll see how to read mapped property value of a bean. We use PropertyUtils.getMappedProperty() method the read the mapped property of the Record object that consist of Track objects.

package org.kodejava.commons.beanutils;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.HashMap;
import java.util.Map;

public class ReadMapProperty {
    public static void main(String[] args) {
        Record record = new Record();
        record.setTitle("Abbey Road");

        Track track1 = new Track();
        track1.setTitle("Come Together");

        Track track2 = new Track();

        Map<String, Track> tracks = new HashMap<>();
        tracks.put("1", track1);
        tracks.put("2", track2);


        try {
            Track track = (Track) PropertyUtils.getMappedProperty(record, "mapTracks(1)");
            System.out.println("track.getTitle() = " + track.getTitle());
        } catch (Exception e) {
package org.kodejava.commons.beanutils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Record {
    private Long id;
    private String title;
    private List<Track> tracks = new ArrayList<>();
    private Map<String, Track> mapTracks = new HashMap<>();

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public List<Track> getTracks() {
        return tracks;

    public void setTracks(List<Track> tracks) {
        this.tracks = tracks;

    public void setMapTracks(Map<String, Track> mapTracks) {
        this.mapTracks = mapTracks;

    public Map<String, Track> getMapTracks() {
        return mapTracks;

And here is the result of our program.

track.getTitle() = Come Together

Maven Dependencies


Maven Central


Leave a Reply

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