Java中的(嵌套)集合的多行漂亮打印 [英] Multi-line pretty-printing of (nested) collections in Java

查看:320
本文介绍了Java中的(嵌套)集合的多行漂亮打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够(漂亮地)打印我的地图内容。

I want to be able to (pretty-)print the contents of my maps.

他们应该有换行符和缩进而不是单行;忽略collections / iterables / etc的toString方法;并递归到嵌套集合中。

They should have newlines and indentation rather than on a single line; ignoring the toString methods of collections/iterables/etc; and recursing into nested collections.

关于地图,我特别感兴趣。我认为JSON可能是相关的,但我不想走那么远,或者至少 - 我不希望我的代码必须知道JSON只是为了让我打印漂亮。我有什么选择(除了自己写这个)?

This is especially of interest for me regarding maps. I suppose JSON'ing might be relevant, but I don't want to go that far, or at least - I don't want my code to have to know about JSON just for me to pretty-print it. What are my options (other than writing this myself)?

推荐答案

你可以使用方法 MapUtils.debugPrint 以打印嵌套映射。

You can use the method MapUtils.debugPrint from the apache commons collections in order to print nested maps.


此方法打印一个格式良好的String,用于描述Map。每个地图条目都将打印出键,值和值类名。当值为Map时,会发生递归行为。

This method prints a nicely formatted String describing the Map. Each map entry will be printed with key, value and value classname. When the value is a Map, recursive behaviour occurs.

这篇关于Java中的(嵌套)集合的多行漂亮打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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