How to use PropertyUtils to get an element from a list inside a map?

Psycho Punch picture Psycho Punch · May 24, 2012 · Viewed 7.7k times · Source

I've been trying to use the indexed notation used for getProperty of PropertyUtils to retrieve an element in a list contained as a map value. Here's an example (I'm using a general syntax here):

map = {"aList": ["elem1", "elem2", "elem3"]}

Let say, I want to get the value "elem2", I'm trying to do it using:

PropertyUtils.getProperty(map, "aList[1]");

but it doesn't seem to work. I always get a null value. Is there another way to do this. To be clear, I know I can do a getProperty("aList").get(0) (after explicitly casting, of course) but I'm working on a solution that needs the code stated above to work.

Answer

Psycho Punch picture Psycho Punch · May 25, 2012

Ok, so I figured it out. The code below works like how I wanted:

PropertyUtils.getProperty(map, "(aList)[1]");

I think, based on this documentation, what I'm doing here is that I'm specifying that aList is a key and not an indexed property of the bean. Something like that.