How do I dump the contents of a hash map?

后端 未结 3 742
难免孤独
难免孤独 2021-01-17 11:02

How can I dump the contents of a Java HashMap(or any other), for example to STDOUT ?

As an example, suppose that I have a complex HashMap of the following structure

相关标签:
3条回答
  • 2021-01-17 11:40

    Use HashMap.toString() (docs here):

    System.out.println("HASH MAP DUMP: " + myHashMap.toString());
    

    Generally, use Object.toString() to dump data like this.

    0 讨论(0)
  • 2021-01-17 11:56

    I often use function like this one ( it might need to be expanded to accommodate prety-print of other types of object inside of Map ).

    @SuppressWarnings("unchecked")
    private static String hashPP(final Map<String,Object> m, String... offset) {
        String retval = "";
        String delta = offset.length == 0 ? "" : offset[0];
        for( Map.Entry<String, Object> e : m.entrySet() ) {
            retval += delta + "["+e.getKey() + "] -> ";
            Object value = e.getValue();
            if( value instanceof Map ) {
                retval += "(Hash)\n" + hashPP((Map<String,Object>)value, delta + "  ");
            } else if( value instanceof List ) {
                retval += "{";
                for( Object element : (List)value ) {
                    retval += element+", ";
                }
                retval += "}\n";
            } else {
                retval += "["+value.toString()+"]\n";
            }
        }
        return retval+"\n";
    } // end of hashPP(...)
    

    Thus following code

    public static void main(String[] cmd) {
        Map<String,Object> document = new HashMap<String, Object>();
    
        Map<String,Object> student1 = new LinkedHashMap<String, Object>();
        document.put("student1", student1);
        student1.put("name", "Bob the Student");
        student1.put("place", "Basement");
        List<Integer> ranking = new LinkedList<Integer>();
        student1.put("ranking", ranking);
        ranking.add(2);
        ranking.add(8);
        ranking.add(1);
        ranking.add(13);
        Map<String,Object> scores1 = new HashMap<String, Object>();
        student1.put("Scores", scores1);
        scores1.put("math", "0");
        scores1.put("business", "100");
    
        Map<String,Object> student2= new LinkedHashMap<String, Object>();
        document.put("student2", student2);
        student2.put("name", "Ivan the Terrible");
        student2.put("place", "Dungeon");
    
        System.out.println(hashPP(document));
    }
    

    will produce output

    [student2] -> (Hash)
      [name] -> [Ivan the Terrible]
      [place] -> [Dungeon]
    
    [student1] -> (Hash)
      [name] -> [Bob the Student]
      [place] -> [Basement]
      [ranking] -> {2, 8, 1, 13, }
      [Scores] -> (Hash)
        [math] -> [0]
        [business] -> [100]
    

    Again. You may need to modify this for your particular needs.

    0 讨论(0)
  • 2021-01-17 11:57

    A good way to dump data structures (e.g. made of nested maps, arrays and sets) is by serializing it to formatted JSON. For example using Gson (com.google.gson):

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

    This will print out the most complex data structures in a fairly readable way.

    0 讨论(0)
提交回复
热议问题