Java

Instantiating an iReport Collection parameter from an Array in one line

While working with iReport, I wanted to test my report with a default Collection value for my Hibernate Query.

The HQL is something like this,


SELECT person.fullName as personName
FROM Person person
WHERE person.id in ($P{people})

The Person.id is a long.
The report parameter $P{people} is a Collection.

To add a default value for testing, I had to rediscover instantiating a Collection from an Array. I was trying to find Arrays.asList(), and the like, but couldn’t remember it at the time.

While searching for what I needed, I found a lot of forums where people would ask how to do instantiation of a Collection from an Array without looping.

If you already have an array (perhaps named ‘myArray’), you could do something like the following.

new ArrayList(Arrays.asList(myArray));

In my case, I didn’t have an array, yet. I also wanted to initialize the array, too. Initializing an array of objects is easy enough.

new Long[] {new Long(521), new Long(423)};

So, combining the two, my default parameter would get


new ArrayList(Arrays.asList(
new Long[] {new Long(521), new Long(423)}
));

Hopefully this will help someone who needs to just instantiate a collection from an array, or someone who needs to figure out how to add a default value for a collection in iReport.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Furl
  • LinkedIn
  • Pownce
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*Required Fields