Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# summation of Nth element from multiple arrayLists for each element.

Monica Shiralkar
Ranch Hand
Posts: 842
1
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.

Tony Docherty
Bartender
Posts: 2958
59
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: 842
1

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.

Winston Gutkowski
Bartender
Posts: 10417
63
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: 842
1
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: 842
1
Thanks.How would an ArrayList of ArrayLists help in doing this?

Tony Docherty
Bartender
Posts: 2958
59
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: 10417
63

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: 842
1
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: 2958
59
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.

Myke Enriq
Ranch Hand
Posts: 115
[Moderator action: Solution removed, for all the reasons mentioned by Tony below. - Winston]

Tony Docherty
Bartender
Posts: 2958
59
@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: 842
1
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: 10417
63
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