Any utility that can quickly print a card

I am wondering which utility quickly prints a card for debugging purposes.

+4
source share
8 answers

You can simply type toString() Map to get a 1-line version of the map divided by key / value records. If this is not readable enough, you can make your own print loop or use Guava to do this:

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

This will give you the result of the form

  key1 -> value1
 key2 -> value2
 ... 
+11
source

I assume that the .toString () method for implementing the class (HashMap or TreeMap for ex.) Will do what you want.

+8
source

Consider: MapUtils (Commons Collection 3.2.1 API)

It has two methods: debugPrint and verbosePrint.

+4
source

How about this:

 Map<String, String> map = new HashMap<String, String>(); for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(map.get(key)); } 

or simply:

 System.out.println(map.toString()); 
+3
source
 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} 
+3
source
  org.apache.commons.collections.MapUtils.debugPrint(System.out, "Print this", myMap); 
+3
source

I think System.out.println works fine with the map, since this:

 Map<String, Integer> map = new HashMap<String, Integer>(); map.put("key1", 1); map.put("key2", 2); System.out.println(map); 

prints:

 {key1=1, key2=2} 

Or you can define the utility method as follows:

 public void printMap(Map<?, ?> map) { for (Entry<?, ?> e : map.entrySet()) { System.out.println("Key: " + e.getKey() + ", Value: " + e.getValue()); } } 
+2
source

Try using StringUtils.join (from Commons Lang )

eg.

 Map<String, String> map = new HashMap<String, String>(); map.put("abc", "123"); map.put("xyz", "456"); System.out.println(StringUtils.join(map.entrySet().iterator(), "|")); 

will create

 abc=123|xyz=456 
+2
source

Source: https://habr.com/ru/post/1347673/


All Articles