M-D M-D - 2 months ago 17
Java Question

How do I dump the contents of a hash map?

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().

Answer

Use HashMap.toString() (docs here):

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

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