Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help Required with Collections

843789Sep 21 2010 — edited Sep 21 2010
Hi,

I have retrieved a number of records from a database that each contain a "code" and a "value". The code is non-unique, e.g. I have multiple records where the code="A", the code ="B" etc.

What I want is a total of all the values per code, e.g. a total of all the values for code A, total of all thecode B values etc.

I can iterate over my records and add the codes to a Set, which gives me a collection of unique codes. Somehow I need to interate over my original list of records and check the code, then add the value to a total. I guess the psedo-code would look a little like this:

for each record
if code = "A", add the value to A's total
if code = "B", add the value to B's total
etc etc.

Is there way I can make this generic?

Sorry for the newbie question. There must be a fairly straightforward way of doing this but I have no idea what it is :(

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2010
Added on Sep 21 2010
1 comment
135 views