convert a map (key, value) into a string - Java java.lang

Java examples for java.lang:char

Description

convert a map (key, value) into a string

Demo Code

/*//  w w w  . j  av a 2  s .c o m
Copyright (c) 2013 eBay, Inc.
This program is licensed under the terms of the eBay Common Development and
Distribution License (CDDL) Version 1.0 (the "License") and any subsequent  version 
thereof released by eBay.  The then-current version of the License can be found 
at http://www.opensource.org/licenses/cddl1.php and in the eBaySDKLicense file that 
is under the eBay SDK ../docs directory.
 */
//package com.java2s;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Main {
    /**
     * convert a map (key, value) into a string
     * @param map
     * @return String
     */
    public static String outputMap(Map map) {
        StringBuffer output = new StringBuffer(" ");
        Set entries = map.entrySet();
        Iterator it = entries.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            output.append(((key == null) ? "null" : key.toString()) + " : "
                    + ((value == null) ? "null" : value.toString()) + "\n");
        }
        return output.toString();
    }
}

Related Tutorials