将java Map转换为custom key = value string [英] Convert java Map to custom key=value string

查看:445
本文介绍了将java Map转换为custom key = value string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TreeMap< String,String> ,我需要转换为类似URI的字符串,然后返回到Map。
我需要设置自定义分隔符。

I have TreeMap<String,String> which I need to convert to URI-like string and then back to Map. I need to set custom delimiters.

有没有可以为我做的工具(Guava,Apache commons?)?我知道,我可以写简单的循环,但是我正在寻找一行:)

Is there any tool (Guava, Apache commons?) that can do it for me? I know, I can write simple loops, but I'm looking for one-liner :)

例如

key    value
key1   val1
key2   val2

key1_val1|key2_val2


推荐答案

根据 http://davidtulig.com/blog/introduction-to-google-guava-s-joiner-class/ 你可以通过

 String string = Joiner.on("|").withKeyValueSeparator("_").join(map);

相反的情况也可通过

 Map<String, String> map = Splitter.on("|").withKeyValueSeparator("_").split(string);

这篇关于将java Map转换为custom key = value string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆