• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looping multiple collections simultaneously?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Tony.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic