Java Map to String toStringMap(Map src)

Here you can find the source of toStringMap(Map src)

Description

to String Map

License

Apache License

Declaration

public static Map<String, String> toStringMap(Map<?, ?> src) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Main {

    public static Map<String, String> toStringMap(Map<?, ?> src) {
        Map<String, String> dst = new HashMap<String, String>();

        Set<?> entrySet = src.entrySet();
        for (Object object : entrySet) {
            if (object != null && object instanceof Entry<?, ?>) {
                Entry<?, ?> entry = (Entry<?, ?>) object;
                Object key = entry.getKey();
                Object value = entry.getValue();

                if (key != null) {
                    if (value == null) {
                        value = "";
                    }//from  w  ww.j  a va  2s .co  m
                    dst.put(key.toString(), value.toString());
                }
            }
        }

        return dst;
    }

    public static String toString(Map<?, ?> src, String format) {
        if (src != null && src.size() > 0) {
            Set<?> keySet = src.keySet();
            for (Object key : keySet) {
                if (key != null) {
                    Object value = src.get(key);
                    if (value != null) {
                        format = format.replaceAll("\\{" + key.toString() + "\\}", value.toString());
                    }
                }
            }
        } else {
            return null;
        }
        return format;
    }
}

Related

  1. toString(Map params)
  2. toString(Map map)
  3. toString(Map map, StringBuilder sb)
  4. toStringMap( Map enumMap)
  5. toStringMap(Map map)
  6. toStringMap(Properties properties)
  7. toStringMap(String... pairs)
  8. toStringMap(String... pairs)