aspose file tools*
The moose likes Java in General and the fly likes how to combine a list of objects into a new list for display? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to combine a list of objects into a new list for display?" Watch "how to combine a list of objects into a new list for display?" New topic
Author

how to combine a list of objects into a new list for display?

twisty fruity
Greenhorn

Joined: Jan 06, 2012
Posts: 1
hello,
i have to display a table onscreen as follows:
product_|_technical doc_|_functional doc_|_user guide_|_admin guide
xbox___|______x______|_____________|_____x____|___________
wii_____|______x______|______x______|__________|____x______

But in my db i have a table which returns the results across multiple lines:
product_|_doc type
xbox___|_technical doc
xbox___|_user guide
wii_____|_technical doc
wii_____|_functional doc
wii_____|_admin guide

how do i combine the lines according to the product so that it displays a single line per product as in the first table? using a loop?
i'm lost :/
any help will be greatly appreciated!
thanks
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Generally there are two possibilities to do this.

1) Let the database do the transformation. This type of query is usually called a pivot query. It is done differently in different databases, so if you don't know how to go about it, try to google it out (or ask here on JDBC forum, but remember to specify your database including version). You'd then obtain rows from the database that would correspond to individual lines in your screen output.

2) Do the transformation in Java (this is what you asked about). I'd probably create a map of sets. The map would have product name as a key, the value would be a set of documentations available for the product. For every record you'd firstly see if that record is already in the map; if not, add it with a new, empty set. Then you'd obtain the set for given product (previous step ensured it would always exist) and add the documentation type to it. Outputting the resulting structure would then be quite straightforward.

If you want to ensure the product and documentations come in sorted order, you can use the TreeMap and TreeSet with proper Comparator to achieve this.
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
If the ordering of the doc_type can be changed on your display, you could also consider adding an orderby clause to the sql. Break to a new output row when product changes which is what I guess you may already be thinking, and populate the 'x' or what docs you have and empty cells for missing doctypes as you iterate through the resultset.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to combine a list of objects into a new list for display?