javacollectionslibrariespretty-print

Multi-line pretty-printing of (nested) collections in Java


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

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.

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)?


Solution

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

    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.