Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Printing out a Map in sorted order

// print out a Map in sorted order of its keys

    public static String dumpToString(Map<String, Object> map) {
        if (map == null) {
            return Constants.EMPTY_STRING;
        }

        StringBuffer strbuf = new StringBuffer();

        SortedMap<String, Object> sMap = new TreeMap<String, Object>(map);
        Set<Map.Entry<String, Object>> s = sMap.entrySet();

        for (Map.Entry<String, Object> elem : s) {
            String key = elem.getKey();
            Object value = elem.getValue();
            strbuf.append(key);
            strbuf.append("=");
            strbuf.append(value == null ? Constants.EMPTY_STRING : value);
            strbuf.append("\r\n");
        }

        return strbuf.toString();
    }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS