• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap Iteration

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I have an HashMap<String,String> childParentMap which looks like below

Child (key) Parent (value)
B A
C B
D C
E A
F E
G F
H B
I H



I need to pass a parent & find the entire hierarchy.

say if i pass A - then i should get
A->B->C->D
A->B->H->I
A->E->F->G

and if i pass B then i should get
B->C->D
B->H->I

Since there can be duplicates present in the parent value of HashMap I am facing difficulty in computing the hierarchy. Any advise please!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use a HashMap as your datastructure. Why create a class that encapsulates the String and has a reference to a child object.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you suggest which collection should I use then? I used Map since it has the Key Value pair.

Also I get the Parent - Child hierarchy easily by starting from the child but find difficult to compute the hierarchy starting from a parent
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first glance that calls for a recursive method. Write a method that takes a Map<String, String> and a String, and:

- Iterate over the entry set looking for matching values
- For each match, call the same method with the key as input
- If there are no matches, exit the method

Then it's a matter of working out what your data structure should be and populating it as you go along. What you really need is a tree structure (each node containing a number of subnodes). For instance, you might be able to use TreeModel from the Swing library if you don't want to write your own.


Of course, what that suggests is that a Map isn't the best structure in the first place, so an alternative (if it's possible) would be to change the code that create the Map.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathew,

Thanks much. I was able to finish my requirement with the recursive method you said



Thanks much!
John
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic