aspose file tools*
The moose likes Beginning Java and the fly likes Looping multiple collections simultaneously? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping multiple collections simultaneously?" Watch "Looping multiple collections simultaneously?" New topic
Author

Looping multiple collections simultaneously?

manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136
Hi,

I have multiple collections in my hand and I need to put their data in a customized object. Collections' size differ, hence in a given customized object
one or more field may not be populated. So I plan to iterate through these collections simultaneously and just put their data in customized object, stopping when largest
collection is exhausted.
I am not able to figure out how to achieve it.
Is my line of attack flawed and I need another approach?

Manish
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

manish ghildiyal wrote:I have multiple collections in my hand and I need to put their data in a customized object. Collections' size differ, hence in a given customized object one or more field may not be populated. So I plan to iterate through these collections simultaneously and just put their data in customized object, stopping when largest collection is exhausted.

So I'm presuming that each Collection contains one field. Right?

I am not able to figure out how to achieve it.
Is my line of attack flawed and I need another approach?

If you're "not able to figure out how to achieve it", does it matter?

However, off the top of my head I'd say that having a single field in each Collection sounds like an odd way to do things.

Perhaps if you could give us more information on what you're trying to do, rather than how you want to do it, we could be of more help.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
Collections' size differ, hence in a given customized object one or more field may not be populated.

How do you know which object an item in a collection should be placed in?
manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136
OK, so I would try to put what I intend to achieve.

I actually have a collection of java objects(say of Class P) whose data I need to display in tabular form(and I am doing this using velocity template).
If fields in object are not collection type then for each java object there is a row in table, and I am happy.
But java object can have collection based fields too, in which case all of the data in collection is getting placed in same box which I don't want. I want a new row for every next value in collection.
So I planned to make a new java class(say A) with only non-collection fields, and then to loop through the collections of original java object(keeping fields null wherever needed) and just put the data in A's instance.
So for every original java object I can have multiple objects of A.
So if P's instance has data as x,y,z,collection1 with values (a,b,c),collection2 with values(m,n)
then there would be A's instances with data as
instance 1:x,y,z,a,m
instance 2:b,n
instance 3:c
I hope I could clarify.

Manish
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
OK. Find the length of the largest collection and have a for loop of that many times.
For each iteration of the loop create a new instance of your storage class A.
For every collection in object P, if the loop counter is less than the size of the collection add the object at that collection's index to the storage object.
manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136
Thanks a lot Tony.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looping multiple collections simultaneously?