Java Map to String toString(Map map)

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

Description

Like Java Map.toString() but no curly braces

License

Open Source License

Declaration

public static String toString(Map<String, ? extends Object> map) 

Method Source Code

//package com.java2s;
/*//  w  ww .  j  a v  a2  s  .c o m
 * Swift Parallel Scripting Language (http://swift-lang.org)
 * Code from Java CoG Kit Project (see notice below) with modifications.
 *
 * Copyright 2005-2014 University of Chicago
 *
 * 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
 *
 *  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.
 */

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**
       Like Java Map.toString() but no curly braces
     */
    public static String toString(Map<String, ? extends Object> map) {

        if (map == null)
            return "null";

        StringBuilder sb = new StringBuilder(map.size() * 128);
        Set<String> keys = map.keySet();
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            String key = it.next();
            Object value = map.get(key);
            sb.append(key);
            sb.append('=');
            sb.append(value);
            if (it.hasNext())
                sb.append(',');
        }
        return sb.toString();
    }

    public static String toString(List<? extends Object> l) {
        if (l == null) {
            return "null";
        }
        return l.toString();
    }
}

Related

  1. toString(Map map)
  2. toString(Map map)
  3. toString(Map map)
  4. toString(Map map)
  5. toString(Map m)
  6. toString(Map map)
  7. toString(Map attrs)
  8. toString(Map attrs)
  9. toString(Map m)