This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashMap, Iteraring problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap, Iteraring problem" Watch "HashMap, Iteraring problem" New topic

HashMap, Iteraring problem

Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
Hi guys,

I have a HashMap, and it stored a bunch of Strings... the key is a String and the value is a Set of String...

and it looks something like this

{ D=[E,C], C=[B], E=[B], B=[A] }

what is the best way to iterate through this map so i get the two list. i.e.

D-E-B-A and the other would be D-C-B-A

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I guess we found D->E, then E->B then B->A? Try writing down what we did in plain English:

Hmmm, that didn't turn out very "plain" because it could keep repeating itself like that.

The solution that comes to mind for me involves "recursion". Are you familiar with how that works? If not, we could go there next.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
not really... someone mentioned something about recursion method... im not familiar with this
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
OP is getting some useful help here, so probably not worth explaining recursion again.

Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
I modified the code(here) slightly so it compiles with 1.4 (without the generics)
and i also havent come accross the


method so i rebuild the graphI'm sticking to what i know for now. so now the code looks like this...

and the output i get is null pointer exception...
why is that?

[ October 02, 2006: Message edited by: Firman Drage ]
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
Ok its solved again... here
I agree. Here's the link:
subject: HashMap, Iteraring problem
It's not a secret anymore!