Solved

HashMaps and parent/child relationships

Posted on 2002-04-30
3
935 Views
Last Modified: 2012-05-04
I need to create a parent to child relationship of my records and would like to use hashmaps to store the data and then a TreeSet/Iterator to get the ordered value

The records are as follows and come in random order:

Record 1: Parent 1; Child 2
Record 2: Parent 2; Child 1
Record 3: Parent 1; Child 1
Record 4: Parent 3; Child 1

The output i'm trying to accomplish is:

Parent 1
     Child 1
     Child 2
Parent 2
     Child 1
Parent 3
     Child 1


I can easily fill the hash maps but can't figure out how to relate the children.

Thanks in advance.  Let me know if more clarification is needed.

Dave
 
0
Comment
Question by:mred_mred
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 75 total points
ID: 6982462
One way to do this would be to create a hashmap of {Parent} --> {SortedSet of children}. You can also have a separate SortedSet of parents or even store them in the hashmap. Here's how you'd add a child:

SortedSet parents = (SortedSet) map.get("root");
Parent p1 = (Parent) parents.first();
SortedSet children = (SortedSet) map.get(p1);
if (children == null) {
  children = new TreeSet();
  map.put(p1,children);
}
children.add(new Child("Child 1"));

To get your output, just iterate through the parents; for each parent, iterate through the SortedSet of children.

Alternatively, and especially if you need more than one level of parent to child relationships, consider creating a Node class which has as a field a SortedSet of children.

0
 

Author Comment

by:mred_mred
ID: 6983835
this definitely answered my question and i did seem to figure it out while waiting.  thanks for the quick response.

i do have more than one level.  where do i find the node class?  or do i create this my self?  class.child_node or something?  or is this a java class that i can use.

thanks
0
 
LVL 7

Expert Comment

by:yoren
ID: 6983902
No, you'd have to create the Node class yourself. A Node could be a parent or a child, depending on if it has any children. Of course, this is assuming that parents and children have basically the same information. In the Node class you'll probably want a "name" field, a "children" field (which would be a SortedSet of other Nodes), and any other information pertinent to a node.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 140
What is the latest versions eclipse neon 2 139
servlet doXXX methods 3 34
print map entry 34 54
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now