I have a arraylist of similar objects (say toys) which I get every day.
List<Toy> toyList;
Toy bean will have four properties say name, type, code, number.
- code could be any integer from 1 to 10 (not necessary that everyday I get all the codes)
- type could be either string “one” or string “two”
I want to output a report summarising the toys on type and code as below:
One
name type number
--------------------------------
abc 1 19
pqr 1 20
Code 1 total 39
lmn 9 15
Code 9 total 15
========================================
Two
name type number
--------------------------------
hmn 6 18
efg 6 20
Code 6 total 38
jkl 5 15
Code 5 total 15
To easily create such a report out of the list I have, I will have to segregate the list at two levels: first based on type (one and two) and then based on the codes available today (all codes will not be available everyday).
What is the most efficient way to achieve this ?
I can achieve it by iterating over the main list and creating sublist. But how to deal with the part that all codes will not be available everyday ? How to create sublists only for the codes available for today ?
Thanks for reading!
You can iterate over all the toys and get the
Setof types, code etc. You might want to make it a SortedSet so that they will appear in a consistent order.