Recursively dump the contents of an aggregate structure (Collection, Array) in Java

This is my very first question on Stackoverflow, so please bear with me if you find something meaningless in my very first post. FYI, that I went through the FREQUENCY FAQ, and I know about its various policies. You see, I’m a guy who has worked with languages ​​such as PHP, Perl, Python, ROR, and I recently switched to context on Java EE. You see, some of the languages ​​I worked on had a construct that allowed me to recursively unload 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 is:

Is there an equivalent to any of these in a Java universe? It’s very convenient for me when I worked in any of these languages ​​(PHP / Perl / Python / ROR) to upload the contents of the composite data structure, to study how it looks, and then process it further (or decide how to process it further)

Thanks for your time and apologies if my post ignores any SO rules.

+7
source share
3 answers

The java.util collections have toString() , which will recursively unload list / set / map details, etc. This requires that the objects stored in the collection be useful toString() .

It is split into arrays because Java does not have a useful default value of toString() for the array. If you know that you are dealing with an array, then you can use the utility method Arrays.deepToString() (see the tip of the manual for more details).

For example, this code:

 final Map<String, Object> map = new HashMap<String, Object>(); final List<String> nested = Arrays.asList("foo", "bar", "baz"); map.put("given", "Arthur"); map.put("family", "Dent"); map.put("nested", nested); System.out.println(map); 

Produces the following:

 {nested=[foo, bar, baz], given=Arthur, family=Dent} 
+1
source

You can use the static methods of the utility found in the java.util.Arrays class:

 public static String toString(Object[] a) ie: String dump = Arrays.toString(array); 

Returns a string representation of the contents of the specified array.

If the array contains other arrays as elements, they are converted to strings by the Object.toString () method, inherited from Object, which describes their identifiers, and not their contents.

The value returned by this method is equal to the value that Arrays.asList (a) .toString () will return if a is not null, in which case "null" is returned.

 public static String deepToString(Object[] a) ie: String dump = Arrays.deepToString(array); 

Returns a string representation of the "deep content" of the specified array. If the array contains other arrays as elements, the string view contains their contents, etc. This method is designed to convert multidimensional arrays to strings.

A string representation consists of a list of array elements enclosed in square brackets ("[]"). Adjacent elements are separated by "," (a comma followed by a space). Elements are converted to strings as if by String.valueOf (Object), if they themselves are not arrays.

In the case of colletions, you can use the toArray () method:

  List<YourObject> list = ..... YourObject[] array = list.toArray(new YourObject[0]); String dump = Arrays.deepToString(array); 
0
source

Depending on your goal, which I intend to debug code or explore a complex data structure, do not print .

Use a good user interface debugger, for example in IntelliJ or (shudder, just remember) an eclipse. Java has a much more mature and complete set of tools available for developers compared to other languages ​​you have listed (I also worked in most of them). Php, Perl, etc. They are primary and for some reason people continue to rely on development in a simple text editor. Java doesn't work that way (at least if you want to stay sane or work on something more than a toy project).

Use the full-featured IDE and the built-in debugger.

In the others you pointed out, this is not a good option for the IDE, and debugging integration is boring at best ... This is one of the main advantages of Java in fact.

0
source

All Articles