The situation occurs when getting some data from the database to compose a report that is grouped by various fields. For example, grouped by month, then type and then actual vs prediction. The database query returns simply a List< Object[] >. But it is boring to directly render it as an HTML table. What I do is to group the data inside a Map<Integer, Map<String, Map<Type, Integer>>>. It can be done in a straightforward way and if I use TreeMap‘s, the data is automatically sorted for me. The resulting JSP code is much simpler.
Is there any problem with this approach? (memory, speed, or something else?)
The HTML renderer shouldn’t have to know the implementation details of you DAO.
I think for you DAO that returning
List<Object[]>is more simple than returningMap<Integer, Map<String, Map<Type, Integer>>>. Every class interface should be easy to understand, so I would useList<Object[]>.If it’s more straightforward to render the HTML table using
Map<Integer, Map<String, Map<Type, Integer>>>maybe is better to create a method to convertList<Object[]>toMap<Integer, Map<String, Map<Type, Integer>>>.This way you keep your UI detached from your database classes.
You also asked about memory and speed. I think the best approach is to test both situations and use a profiler to measure if this is really an issue.