Any utility which can print out map quickly

16,007

Solution 1

You can just print the toString() of a Map to get a 1-line version of the map, divided up in to key/value entries. If that isn't readable enough, you could do your own looping to print or use Guava to do this:

System.out.println(Joiner.on('\n').withKeyValueSeparator(" -> ").join(map));

That'll give you output of the form

key1 -> value1
key2 -> value2
...

Solution 2

I guess, the .toString() method of implementing class (HashMap or TreeMap for ex.) will do what you want.

Solution 3

Consider: MapUtils (Commons Collection 4.2 API)

It has two methods: debugPrint & verbosePrint.

Solution 4

  org.apache.commons.collections.MapUtils.debugPrint(System.out, "Print this", myMap);

Solution 5

public final class Foo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        System.out.println(map);
    }
}

Output:

{key2=value2, key1=value1}
Share:
16,007

Related videos on Youtube

user496949
Author by

user496949

Updated on June 04, 2022

Comments

  • user496949
    user496949 almost 2 years

    I am wondering any utility to print out map quickly for debugging purpose.

    • Admin
      Admin about 13 years
      You could use an iterator and for loop to print out the members in 2 lines.
  • Nicolas Raoul
    Nicolas Raoul about 7 years
    Example output from HashMap.toString: {key1=value1, key2=value2}. That's well enough for debugging purposes.

Related