Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 710
einpoklum Avatar asked Mar 02 '13 06:03

einpoklum


2 Answers

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.

like image 138
lbalazscs Avatar answered Nov 15 '22 17:11

lbalazscs


Try replacing the start of each entry with new line and tab like this

myMap.toString().replace("[", "\n\t[");

like image 4
Ravindra Gullapalli Avatar answered Nov 15 '22 18:11

Ravindra Gullapalli