This is my very first question on Stackoverflow, so please bear with me if you find anything nonsensical in my very first post. FYI that I’ve gone through the SO FAQ and I’m aware of its various policies. You see, I’m a guy that has heavily worked with languages such as PHP, Perl, Python, ROR and recently I “context-switched” to Java EE. You see, several of the languages that I’ve worked on had a construct that enabled me to recursively dump the contents of an aggregate structure without a for/foreach/for..in loop
For example,
a = Any composite Data structure
PHP has
var_dump() and print_r()
Perl has
Data::Dumper
ROR has
PrettyPrint
and Python has
pprint module
.
So my question goes,
Is there an equivalent of any of these in the Java universe ? I find it very convenient when I used to work in any of these languages (PHP/Perl/Python/ROR) to dump the contents of an composite data-structure to examine how it looks and then process it further (or decide on how to process it further)
Thanks for your time and apologies if my post overlooks any SO rules.
The
java.utilcollections havetoString()that will recursively dump out the details of the list / set / map etc. This requires that the objects stored in the collection have usefultoString()s defined.It breaks down for arrays however, because Java doesn’t have a useful default
toString()for an array. If you know you’re dealing with an array, then you can use theArrays.deepToString()utility method (see guido’s response for more detail).For example, this code:
Produces this: