• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic