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

Printing out a Map in sorted order (See related posts)

// 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();
    }

You need to create an account or log in to post comments to this site.


Click here to browse all 4857 code snippets

Related Posts