This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap, Iteraring problem" Watch "HashMap, Iteraring problem" New topic
Author

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


MV
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
Rancher

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


Joanne
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

Arrays.asList

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HashMap, Iteraring problem