[ACCEPTED]-Java: print_r?-data-structures

Accepted answer
Score: 10

Calling toString on the collection should 5 return a string containing all the elements 4 string representations.

This won't work with 3 built-in arrays though, as they don't have 2 a toString override and will just give you 1 a memory address.

Score: 10

There is really difference between toString() in 20 java and print_r() in PHP. Note that there 19 is also __toString() in php which is equivalent 18 to toString() in java, so this is not really 17 the answer.

print_r is used when we have 16 a structure of objects and we like quickly 15 to see the complete graph of objects with 14 their values.

Implementing toString in 13 java for each object has not the power to 12 compare with print_r.

Instead use gson. It 11 does the same job as print_r

Gson gson = new 10 GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(someObject));

In 9 this way you do not need to implement toString 8 for every object you need to test it.

Here 7 is the documentation: http://sites.google.com/site/gson/gson-user-guide

Demo classes (in 6 java):

public class A {

int firstParameter = 0;
B secondObject = new B();

}

public class B 5 {

String myName = "this is my name";  

}

Here is the output in php with print_r:

Object
(
[firstParameter:private] => 0
[secondObject:private] => B 4 Object
(
[myName:private] => this 3 is my name
)

)

Here is the output 2 in java with gson:

{
"firstParameter": 0,
"secondObject": {
"myName": "this 1 is my name"
}
}

Score: 8

Depending on exactly what you want to do, the 6 solution could be fairly simple. The following 5 won't produce the formatted output that 4 print_r provides, but it will allow you 3 to output the structure of lists, arrays, and 2 maps:

    // Output a list
    final List<String> list = new ArrayList<String>();
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
    System.out.println(list);

    // Output an array
    final String[] array = {"four", "three", "two", "one"};
    System.out.println(Arrays.asList(array));

    // Output a map
    final Map<String, String> map = new HashMap<String, String>();
    map.put("one", "value");
    map.put("two", "value");
    map.put("three", "value");
    System.out.println(map.entrySet());

For other types of objects you could 1 use reflection to create a utility for this purpose.

Score: 7

You might try the good old Apache Commons 1 Lang's ToStringBuilder.

Score: 2

Others have mentioned the toString() method. This 13 is really only useful when the object implements 12 toString(). If it has not been implemented properly 11 you will end up with something like this: java.lang.Object@1b9240e.

Even 10 if it is a little tedious it is still easy 9 to implement toString() in your own classes, but third 8 party classes will not always implement 7 it. You are much better off using a debugger. The 6 only reason things like print_r even exist in PHP 5 is because of the lack of a real debugger. I 4 think you will find that being able to set 3 breakpoints instead of using a bunch of 2 diagnostic print statements will result 1 in a much faster workflow and cleaner code.

Score: 2

My take for a Java print_r like functionality, is 12 the class RecursiveDump that I wrote. It's not perfect, but 11 it is working well for me. The usage is:

String output = RecursiveDump.dump(...);

It 10 could be improved using generics, I wrote 9 it many years ago, before I knew about them. Also 8 while it tries to deal with some Collection 7 types, with Objects it will just call the 6 toString() method.

I thought to use Reflection to 5 extract field names for classes without 4 a proper toString(), but since Spring Roo @RooToString can write the toString() method 3 for you...

Of course the alternative is to 2 use the inspector tool of a debugger, but 1 sometimes is quicker to use prints.

Score: 0

I don't know of an equivalent of print_r 6 in java.

But...

Every object has a default 5 implementation of the toString() method, the 4 list and map implementations print their 3 contents if you call toString() for them.

If 2 you need any debug information printed out, toString() may 1 be the place you are looking for.

Score: 0

There is no equivalent for print_r in Java.

But 2 for maps or lists you can use the foreach-loop 1 like this:

List <String> list = …; // fills your list 

// print each list element 

for (String s : list) {

   System.out.println(s);

 }
Score: 0

For arrays the easiest way is Arrays.asList(array).toString() . I 4 generally implement toString() on objects 3 I like to habe in debug outputs. For generated 2 objects (JAXB etc.) though might need to 1 make an utility class to print it.

More Related questions