Hi,
creating an array and copying all items from a collection into it
is more expensive as a simple iterator walktrough with casts
on each item.
| Collection codes = ahome.findAll();
| MyEntityLightValue[] myvo = new MyEntityLightValue[codes.size()];
|
| Iterator iterator = codes.iterator();
| for(int index=myvo.length; index>0; index--)
| {
| myvo[index] = ((MyEntityLocal)iterator.next()).getMyEntityLightValue();
| }
|
*note there is no iterator.hasNext() as break condition but
a simple counter because you know, how many times you can
invoke iterator.next().
For large collections you can access the item buffer of a
collection directly without the costs of invoking a
ArrayList.get(index) or iterator.next() at all.
| Field elementDataField = ArrayList.class.getDeclaredField("elementData");
| elementDataField.setAccessible(true);
|
| Object myEntities[] = (Object[])elementDataField.get(ahome.findAll());
| MyEntityLightValue[] myvo = new MyEntityLightValue[myEntities.length];
|
| for(int index=0; index<myvo.length; index++)
| {
| myvo[index] = ((MyEntityLocal)myEntities[index]).getMyEntityLightValue();
| }
| return myvo;
|
or even reuse the buffer and return an array with value objects.
(I assume the collection is garbage collected after the method call)
| Field elementDataField = ArrayList.class.getDeclaredField("elementData");
| elementDataField.setAccessible(true);
|
| Object myEntities[] = (Object[])elementDataField.get(ahome.findAll());
|
| for(int index=0; index<myEntities.length; index++)
| {
| myEntities[index] = ((MyEntityLocal)myEntities[index]).getMyEntityLightValue();
| }
|
| return myEntities; // Object[]
|
Regards,
Michael
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3830062#3830062
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3830062
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user