Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Loop

 
John Seals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings:

I am building a "tree view" and I need to be able to display the information in a specific way. Let's say my data looks like this:

(imagine this as a two column database)
a 1
a 2
a 3
b 4
b 5
b 6

But i need to display it like this:
a: 1
2
3

b: 4
5
6

What is the best way to do this? I don't think that this is too easy in SQL so I am trying to figure out how to do a loop in my servlet to build this. Do I write a loop with a conditional of some kind?

I am thinking this has a simple solution but I am not seeing it.

Muchas gracias!
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.util.Map<String,List<String>> (you can use one of it's implementations, java.util.HashMap perhaps) to store data. I assume you know how to get the data from database?
 
Mohamed Khamis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store the first column in a string, for example

 
John Seals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are both great tips! Thanks for the quick replies gentlemen.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Mohamed Khamis
 
Mohamed Khamis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to JavaRanch, Mohamed Khamis


Thanks glad to be here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic