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.