Java Map to String mapToString(Map map)

Here you can find the source of mapToString(Map map)

Description

Converts the given map of objects to string as a comma-separated list.

License

Open Source License

Parameter

Parameter Description
map Map to be converted

Return

A string containing each object in the given map, converted toString() and separated by commas.

Declaration

public static String mapToString(Map map) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2003 Andy Jefferson and others. All rights reserved. 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at//from   w w  w.  ja  va 2s  .c o m
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
     
    
Contributors:
2003 Erik Bengtson - moved replaceAll from Column class to here
2004 Andy Jefferson - moved intArrayToString, booleanArrayToString from SM
2007 Xuan Baldauf - toJVMIDString hex fix
...
**********************************************************************/

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * Converts the given map of objects to string as a comma-separated list.
     * If the map is empty the string "<none>" is returned.
     * @param map Map to be converted
     * @return  A string containing each object in the given map, converted toString() and separated by commas.
     */
    public static String mapToString(Map map) {
        if (map == null) {
            return "<null>";
        } else if (map.isEmpty()) {
            return "<none>";
        } else {
            StringBuilder s = new StringBuilder();
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (s.length() > 0) {
                    s.append(", ");
                }

                s.append("<" + entry.getKey() + "," + entry.getValue() + ">");
            }

            return s.toString();
        }
    }

    /**
     * check string is null or length is 0.
     * @param s check string 
     * @return return true if string is null or length is 0. return false other case.
     */
    public static boolean isEmpty(String s) {
        return ((s == null) || (s.length() == 0));
    }
}

Related

  1. mapToStr(Map map)
  2. mapToStr(Map map)
  3. mapToString(final Map m)
  4. mapToString(final Map m)
  5. mapToString(final Map map)
  6. mapToString(Map map)
  7. mapToString(Map map)
  8. mapToString(Map map)
  9. mapToString(Map map)