Java Map Print printMap(String mapLabel, Map map)

Here you can find the source of printMap(String mapLabel, Map map)

Description

Debugging support -- Print contents of a map (HashMap, TreeMap, etc.).

License

Open Source License

Parameter

Parameter Description
mapLabel Label for map.
map The map to print. <p> N.B. This method assumes both the keys and values have toString() methods. </p>

Declaration


public static void printMap(String mapLabel, Map map) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.util.*;

public class Main {
    /** Debugging support -- Print contents of a map (HashMap, TreeMap, etc.).
     *// w w  w .ja  v  a 2 s .c om
     *   @param   mapLabel      Label for map.
     *
     *   @param   map            The map to print.
     *
     *   <p>
     *   N.B.  This method assumes both the keys and values have toString() methods.
     *   </p>
     */

    public static void printMap(String mapLabel, Map map) {
        if (map == null) {
            System.out.println(mapLabel + " is null.");
        } else if (map.size() == 0) {
            System.out.println(mapLabel + " is empty.");
        } else {
            System.out.println(mapLabel);

            Iterator iterator = map.keySet().iterator();

            int i = 0;

            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);

                if (key == null) {
                    if (value == null) {
                        System.out.println(i + ": null=null");
                    } else {
                        System.out.println(i + ": null=" + value.toString());
                    }
                } else {
                    if (value == null) {
                        System.out.println(i + ": " + key.toString() + "=null");
                    } else {
                        System.out.println(i + ": " + key.toString() + "=" + value.toString());
                    }
                }

                i++;
            }
        }
    }
}

Related

  1. printMap(Map map)
  2. printMap(Map map)
  3. printMap(Map map)
  4. printMap(Map map)
  5. printMap(Map map, int depth)
  6. printMapGeneral(Map map)
  7. printMapObject(Map mapobject)
  8. printMapStrings(Map> map, String mapDescription)
  9. printMapToString(Map map)