java8 Map转String工具类

public class MapToStringUtil {
    /**
     * Map转String工具
     * @param map
     * @param separator 分隔符
     * @param kvSplice  键值拼接符
     * @return
     */
    public static String mapToString(Map<?, ?> map, String separator, String kvSplice) {
        List<String> result = new ArrayList<>();
        map.entrySet().parallelStream().reduce(result, (first, second)->{
            first.add(second.getKey() + kvSplice + second.getValue());
            return first;
        }, (first, second)->{
            if (first == second) {
                return first;
            }
            first.addAll(second);
            return first;
        });

        return StringUtils.join(result, separator);
    }

    public static void main(String[] args) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("name", "hjz");
        map.put("value", 25);

        System.out.println(MapToStringUtil.mapToString(map, ";", ":"));
        //name:hjz;value:25
    }
}

 

    原文作者:king_qc
    原文地址: https://blog.csdn.net/king_qc/article/details/82907175
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞