Question

last time I asked how to populate a data structure here. Now I would like to know if there's something in Java, like the print_r I use in PHP, to represent what I have populated in the Maps and lists without having to do my own algorithm.

Any ideas?

Was it helpful?

Solution

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

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

OTHER TIPS

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

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

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

Instead use gson. It does the same job as print_r

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

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

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

Demo classes (in java):

public class A {

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

}

public class B {

String myName = "this is my name";  

}

Here is the output in php with print_r:

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

)

Here is the output in java with gson:

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

Depending on exactly what you want to do, the solution could be fairly simple. The following won't produce the formatted output that print_r provides, but it will allow you to output the structure of lists, arrays, and 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 use reflection to create a utility for this purpose.

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

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

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

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

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

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

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

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

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

But...

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

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

There is no equivalent for print_r in Java.

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

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

// print each list element 

for (String s : list) {

   System.out.println(s);

 }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top