18

How can I dump the contents of a Java HashMap(or any other), for example to STDOUT ?

As an example, suppose that I have a complex HashMap of the following structure :

( student1 => Map( name => Tim,         
                   Scores => Map( math => 10,
                                  physics => 20,
                                  Computers => 30),
                   place => Miami,
                   ranking => Array(2,8,1,13),
                  ),
 student2 => Map ( 
                   ...............
                   ...............
                 ),
............................
............................
);

So I would like to print it to the screen in order to get an idea about the data structure. I am looking for something similar to PHP's var_dump() or Perl's dumper().

M-D
  • 10,057
  • 9
  • 29
  • 35

3 Answers3

30

Use HashMap.toString() (docs here):

System.out.println("HASH MAP DUMP: " + myHashMap.toString());

Generally, use Object.toString() to dump data like this.

pb2q
  • 56,563
  • 18
  • 143
  • 144
  • It works fine except for arrays. See a print here, `{list_mem={validate_data={name=STRING, place=DONT_KNOW}, func=getMemList, elems=[I@973678}}`. Here `elems` is an array and its values are not printed. – M-D Jun 13 '12 at 04:47
3

A good way to dump data structures (e.g. made of nested maps, arrays and sets) is by serializing it to formatted JSON. For example using Gson (com.google.gson):

Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(dataStructure));

This will print out the most complex data structures in a fairly readable way.

jox
  • 2,013
  • 20
  • 31
0

I often use function like this one ( it might need to be expanded to accommodate prety-print of other types of object inside of Map ).

@SuppressWarnings("unchecked")
private static String hashPP(final Map<String,Object> m, String... offset) {
    String retval = "";
    String delta = offset.length == 0 ? "" : offset[0];
    for( Map.Entry<String, Object> e : m.entrySet() ) {
        retval += delta + "["+e.getKey() + "] -> ";
        Object value = e.getValue();
        if( value instanceof Map ) {
            retval += "(Hash)\n" + hashPP((Map<String,Object>)value, delta + "  ");
        } else if( value instanceof List ) {
            retval += "{";
            for( Object element : (List)value ) {
                retval += element+", ";
            }
            retval += "}\n";
        } else {
            retval += "["+value.toString()+"]\n";
        }
    }
    return retval+"\n";
} // end of hashPP(...)

Thus following code

public static void main(String[] cmd) {
    Map<String,Object> document = new HashMap<String, Object>();

    Map<String,Object> student1 = new LinkedHashMap<String, Object>();
    document.put("student1", student1);
    student1.put("name", "Bob the Student");
    student1.put("place", "Basement");
    List<Integer> ranking = new LinkedList<Integer>();
    student1.put("ranking", ranking);
    ranking.add(2);
    ranking.add(8);
    ranking.add(1);
    ranking.add(13);
    Map<String,Object> scores1 = new HashMap<String, Object>();
    student1.put("Scores", scores1);
    scores1.put("math", "0");
    scores1.put("business", "100");

    Map<String,Object> student2= new LinkedHashMap<String, Object>();
    document.put("student2", student2);
    student2.put("name", "Ivan the Terrible");
    student2.put("place", "Dungeon");

    System.out.println(hashPP(document));
}

will produce output

[student2] -> (Hash)
  [name] -> [Ivan the Terrible]
  [place] -> [Dungeon]

[student1] -> (Hash)
  [name] -> [Bob the Student]
  [place] -> [Basement]
  [ranking] -> {2, 8, 1, 13, }
  [Scores] -> (Hash)
    [math] -> [0]
    [business] -> [100]

Again. You may need to modify this for your particular needs.

Dmitri
  • 114
  • 5
  • Definitely use `StringBuilder` instead of concatenation. Also what's the point of `offset` being varargs? And the mix of arbitrary generic type parameters, raw types and unsafe casts? – Paul Bellora Jun 13 '12 at 04:11
  • 1) If one uses this function for occasional debugging then using string concatenation does not make any difference. 2) Having offset variable as varagrs I avoid creating another function hashPP(Map m) { return hashPP(m, ""); } – Dmitri Jun 13 '12 at 05:18
  • Also, since I use this function for debug printout, I know that most of my code key is usually a string and value is some generic object or another hashmap of similar nature, thus following works for majority of the cases. To make it more generic one would make key an Object as well. – Dmitri Jun 13 '12 at 05:29