Traversing Through/Printing out a Binary Tree Map in Java

So I have this Binary Tree Map (called "mainMap") that holds strings as its keys and each key has an inner map as its value pair. This map within a map holds string keys and integer values.

The purpose of this mainMap is to tell which word corresponds to which file and its frequency. e.g.: If "cat" is in "Arnold.txt" 14 times and it is in "Jake.txt" 6 times, the map will hold

key .... cat value ... Map [key ... Arnold.txt, value ... 14], [key .... Jake.txt, value ... 6]

This is no problem. My problem is TRAVERSING THROUGH AND PRINTING OUT the map. I want to

--For each key in the map 1) Visit the current key within the map 2) Print out that key 3) Retrieve the value (map) .........For each key in the value map .............a) Print out the key .............b) Print out the value 4) Skip a line, go to the next key, repeat steps 1-4

I was thinking I could use an iterator. However, much to my embarrassment, I have no clue how to go about the syntax with my code. (I am not using java's built in TreeMap utilities. I am using a programmer constructed data structure.

I have the following method within the Binary Tree Map code

public Iterator< KeyValuePair<K, E> > iterator()
{
    Iterator< KeyValuePair<K, E> > itr = new MyTreeItr();
    return itr;
}

public Iterator< KeyValuePair<K, E> > breadthFirstIterator()
{
   Iterator< KeyValuePair<K, E> > itr = new MyBreadthFirstItr();
   return itr;
}

I hope this is enough information. This print out is for testing purposes to make sure my map is collecting the data correctly.

Answers


I'm not sure how your tree is structured, but based on how you've described your data structure (Map<String, Map<String, Integer>>), you can try this:

public void printAll(Map<String, Map<String, Integer>> map){
    for(Map.Entry<String, Map<String,Integer>> entry : map.entrySet()){
        System.out.println(entry.getKey());
        for( Map.Entry<String, Integer> innerEntry : entry.getValue().entrySet()){
             System.out.println(innerEntry.getKey());
             System.out.println(innerEntry.getValue());
        }
        System.out.println("");
    }
}

Need Your Help

Why can't a stored procedure read a table from another database (I must be using GRANT and DENY wrongly)

sql stored-procedures database-permissions

I have two Microsoft SQL Server 2000 databases, and a stored procedure on one tries to read data from the other. This used to work fine, but since I became security-conscious and changed the login ...

Generate RSA to existent validation

c# rsa sha1

I need to generate a new license file fro a customer.