wood burning stoves 2.0*
The moose likes Java in General and the fly likes Retrieving list from a map mapping to an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Retrieving list from a map mapping to an arraylist" Watch "Retrieving list from a map mapping to an arraylist" New topic
Author

Retrieving list from a map mapping to an arraylist

Mitesh Parmar
Greenhorn

Joined: Sep 02, 2004
Posts: 2
I have a map datastructure in which keys map to an arraylist. The values of the arraylists elements themselves are keys within the map.

For example:

Map List of previous keys
N ----------------> M
M ----------------> I, L
L ----------------> K
K ----------------> J
J ----------------> D
D ----------------> B
I ----------------> G
G ----------------> F,H
H ----------------> D
F ----------------> C
C ----------------> B
B ----------------> A

So from this we can see that to get from the end: N to the start: A
there are 3 possible routes:

N -> M -> I -> G -> F -> C -> B -> A
N -> M -> I -> G -> H -> D -> B -> A
N -> M -> L -> K -> J -> D -> B -> A

I need the above three routes returned in an ArrayList which are all held in a List : List routeCollections

The way to get this done involves recursion, I have the code written in Java but it does not work beyond creating the first route.



This only works for the first run.

Please help...
[ September 03, 2004: Message edited by: Mitesh Parmar ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Your example looks like a tree to me.
But perhaps other examples are only (distinct) graphs:

or contain recursions:


And please edit your post and use code-tags.


http://home.arcor.de/hirnstrom/bewerbung
Mitesh Parmar
Greenhorn

Joined: Sep 02, 2004
Posts: 2
Originally posted by Stefan Wagner:
Your example looks like a tree to me.
But perhaps other examples are only (distinct) graphs:

or contain recursions:


And please edit your post and use code-tags.


PLease could you tell me how to edit the code and add the code tags.
Thank You...
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

On top of your posting is a icon-group.
One shows a piece of paper with a pencil.
That's for editing.

Before inserting code, you hit the code-button below the edit-window.
You insert your code between the two tags.
Here I inserted blanks into the tags, to make them visible:
[ CODE] show (example);[ /CODE]
Result without blanks:

You may add these tags manually.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

The code-tags make it much better readable - don't you think so?

But your question remains unresponded.
I have to correct myself - the data-structure isn't a tree, it's a directed graph.
I thought about it a while and tested some code but it isn't working properly.
To reconstruct your error, I would need the missing code at least.

I could send you my code - 136 rows in java-1.5 notation as private message. (Guess it's a bit much for the whole audience).

I get:

, the 'A' allways missing, and the forking a step off.
But at least three lines.
[ September 03, 2004: Message edited by: Stefan Wagner ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Now I got a working approach - interested?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving list from a map mapping to an arraylist