Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap, Iteraring problem

 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not really... someone mentioned something about recursion method... im not familiar with this
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP is getting some useful help here, so probably not worth explaining recursion again.
 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok its solved again... here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic