如何转储哈希映射的内容? [英] How do I dump the contents of a hash map?

查看:113
本文介绍了如何转储哈希映射的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Java HashMap(或任何其他)的内容转储到STDOUT?

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

作为一个例子,假设我有一个复杂的HashMap以下结构:

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

所以我想把它打印到屏幕上,以便了解数据结构。
我正在寻找类似于PHP的var_dump()或Perl的dumper()的东西。

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

推荐答案

使用 HashMap.toString() docs here ):

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

一般来说,使用 Object.toString()转储这样的数据。

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

这篇关于如何转储哈希映射的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆