java 有没有一种方便的方法可以将逗号分隔的字符串转换为哈希图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14097515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:03:17  来源:igfitidea点击:

Is there a convenient way to convert comma separated string to hashmap

java

提问by secondflying

String format is (not json format):

字符串格式为(非json格式):

a="0PN5J17HBGZHT7JJ3X82", b="frJIUN8DYpKDtOLCwo/yzg="

I want convert this string to a HashMap:

我想将此字符串转换为 HashMap:

key awith value 0PN5J17HBGZHT7JJ3X82

a与值0PN5J17HBGZHT7JJ3X82

key bwith value frJIUN8DYpKDtOLCwo/yzg=

b与值frJIUN8DYpKDtOLCwo/yzg=

Is there a convenient way? Thanks

有没有方便的方法?谢谢

What I've tried:

我试过的:

    Map<String, String> map = new HashMap<String, String>();
    String s = "a=\"00PN5J17HBGZHT7JJ3X82\",b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
    String []tmp = StringUtils.split(s,',');
    for (String v : tmp) {
        String[] t = StringUtils.split(v,'=');
        map.put(t[0], t[1]);
    }   

I get this result:

我得到这个结果:

key awith value "0PN5J17HBGZHT7JJ3X82"

a与值"0PN5J17HBGZHT7JJ3X82"

key bwith value "frJIUN8DYpKDtOLCwo/yzg

b与值"frJIUN8DYpKDtOLCwo/yzg

for key a, the start and end double quotation marks(") is unwanted; for key b, the start double quotation marks(") is unwanted and the last equals sign(=) is missing. Sorry for my poor english.

对于 key a,开始和结束双引号 (") 是不需要的;对于 key b,开始双引号 (") 是不需要的,并且缺少最后一个等号 (=)。对不起,我的英语不好。

回答by Ryan Stewart

Probably you don't care that it's a HashMap, just a Map, so this will do it, since Properties implements Map:

可能你不在乎它是一个 HashMap,只是一个 Map,所以这会做到,因为 Properties 实现了 Map:

import java.io.StringReader;
import java.util.*;

public class Strings {
    public static void main(String[] args) throws Exception {
        String input = "a=\"0PN5J17HBGZHT7JJ3X82\", b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
        String propertiesFormat = input.replaceAll(",", "\n");
        Properties properties = new Properties();
        properties.load(new StringReader(propertiesFormat));
        System.out.println(properties);
    }
}

Output:

输出:

{b="frJIUN8DYpKDtOLCwo/yzg=", a="0PN5J17HBGZHT7JJ3X82"}

If you absolutely need a HashMap, you can construct one with the Properties object as input: new HashMap(properties).

如果你绝对需要一个HashMap,你可以构造一个与属性对象作为输入:new HashMap(properties)

回答by Kumar Vivek Mitra

Split the String on the Basis of commas (",")and then with with ("=")

在基础上拆分字符串commas (","),然后使用("=")

String s = "Comma Separated String";
HashMap<String, String> map = new HashMap<String, String>();

String[] arr = s.split(",");

String[] arStr = arr.split("=");

map.put(arr[0], arr[1]);

回答by vels4j

Added few changes in Ryan's code

在 Ryan 的代码中添加了一些更改

 public static void main(String[] args) throws Exception {
        String input = "a=\"0PN5J17HBGZHT7JJ3X82\", b=\"frJIUN8DYpKDtOLCwo/yzg=\"";
        input=input.replaceAll("\"", "");
        String propertiesFormat = input.replaceAll(",", "\n");
        Properties properties = new Properties();
        properties.load(new StringReader(propertiesFormat));
        Set<Entry<Object, Object>> entrySet = properties.entrySet();
        HashMap<String,String > map = new HashMap<String, String>();
        for (Iterator<Entry<Object, Object>> it = entrySet.iterator(); it.hasNext();) {
            Entry<Object,Object> entry = it.next();
            map.put((String)entry.getKey(), (String)entry.getValue());
        }
        System.out.println(map);
    }

回答by Sandeep

You can also use the regex as below.

您还可以使用如下正则表达式。

Map<String,String> data = new HashMap<String,String>();
Pattern p = Pattern.compile("[\{\}\=\, ]++");
String[] split = p.split(text);
for ( int i=0; i+2 <= split.length; i+=2 ){
    data.put( split[i], split[i+1] );
}
return data;