This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Nested Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Loop" Watch "Nested Loop" New topic

Nested Loop

John Seals

Joined: Dec 30, 2008
Posts: 5

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

b: 4

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

Joined: Mar 24, 2008
Posts: 3775

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?

[How to ask questions] [Twitter]
Mohamed Khamis

Joined: Oct 18, 2009
Posts: 3
You can store the first column in a string, for example

John Seals

Joined: Dec 30, 2008
Posts: 5
These are both great tips! Thanks for the quick replies gentlemen.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36579
Welcome to JavaRanch, Mohamed Khamis
Mohamed Khamis

Joined: Oct 18, 2009
Posts: 3
Campbell Ritchie wrote:Welcome to JavaRanch, Mohamed Khamis

Thanks glad to be here.
I agree. Here's the link:
subject: Nested Loop
Similar Threads
paging help
How to make combination function generic
Some questions on servlet connected to mysql
2 Dimentional Array
MyPattern.Java - homework help?