How do I dump the contents of a hash map?

后端 未结 3 744
难免孤独
难免孤独 2021-01-17 11:02

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 11:57

    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.

提交回复
热议问题