• 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

summation of Nth element from multiple arrayLists for each element.

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 30 arrayLists in a program. I need to do sum of 1st element of each arrayList,2nd element of each arrayList and so on. Had it been 2 arrayList it would have been possible by iterating one list within the another.Since the arrayLists are too many iterating one list within another does not look feasible. Please suggest what would be the best approach in this case.

Thanks.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all the arraylists have the same number of elements then you can do it with nested for loops. The outer loop for the number of elements and the inner loop for the number of arraylists.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks but how to do this. I did not understand :

The outer loop for the number of elements and the inner loop for the number of arraylists.



Also I do not have 1 or 2 but 30 arrayLists.
 
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

Monica. Shiralkar wrote:Also I do not have 1 or 2 but 30 arrayLists.


So that might be a good place to start.

Why do you have 30 ArrayLists? Could this be increased to 50, or 100 or 1000? What would you do then?

If the likelihood is that this won't change, I'd suggest an array of ArrayLists. If it will, then probably an ArrayList of ArrayLists would be better.

But before you do anything, ask yourself: Why do I have 30 Lists?

I've never written a program with 30 lists in it - in any language - and I've been programming for 35 years.

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in hadoop the mapper emits key value pair and each value is arrayList of values against a given key..So once we have 30 arrayList how to proceed.Iterating one within another is not feasible solution.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.How would an ArrayList of ArrayLists help in doing this?
 
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
Oh I see, you mean your 30 arraylists are separate variables and not an array or list of arraylists
The first thing to do is to change your code from dealing with individual arraylist variables to dealing with a collection of arraylists eg a List or array of arraylists and then you iterate over that.
 
Winston Gutkowski
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

Monica. Shiralkar wrote:in hadoop


A rather vital piece of information. In future, you might do well to remember stuff like this.

the mapper emits key value pair and each value is arrayList of values against a given key..So once we have 30 arrayList how to proceed.Iterating one within another is not feasible solution.


So, why not just process each ArrayList as it comes in? Or is there some other reason that makes you think that you need to gather them all together first?

I'm afraid we're still a few lines short of a full drawing here.

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All.

The first thing to do is to change your code from dealing with individual arraylist variables to dealing with a collection of arraylists eg a List or array of arraylists and then you iterate over that.



Now suppose I have an array of arrayLists say 30 arrayLists.How do I Iterate the arrayList to get top 1 of each arrayList,then top 2 of each arrayList and so on? How would 1 iterator do that?



So, why not just process each ArrayList as it comes in? Or is there some other reason that makes you think that you need to gather them all together first?



It would arrive in Reducer class method as 30 arrayLists only in this case and calculation starts from there.So I need to find a way.
 
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

Monica. Shiralkar wrote:
Now suppose I have an array of arrayLists say 30 arrayLists.How do I Iterate the arrayList to get top 1 of each arrayList,then top 2 of each arrayList and so on? How would 1 iterator do that?


I explained how to do that in my first reply.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Moderator action: Solution removed, for all the reasons mentioned by Tony below. - Winston]
 
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
@Myke Enriq: Please do not post complete solutions, the aim is to guide the OP so they can code the solution themselves.
And if you are going to post code please use code tags and provide code that compiles.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am taking myArrayList.get[0] and summing all,next I am doing myArrayList.get[1] and sum all. This way it should work hopefully.

Thanks all.
 
Winston Gutkowski
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

Monica. Shiralkar wrote: I am taking myArrayList.get[0] and summing all,next I am doing myArrayList.get[1] and sum all. This way it should work hopefully.


You shouldn't hope, you should know.

Also, if you're simply processing myArrayList elements in sequence, there's a much better way to do it.
(Hint: ArrayList - indeed, all Java Collections - are Iterable (java.lang.Iterable ←click).

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic