【问题标题】:Convert value in string into hashmap?将字符串中的值转换为哈希图?
【发布时间】:2017-02-15 13:50:48
【问题描述】:

我有一个字符串,其中有多个值。键和值用*分隔,整个值用$分隔。

下面是例子:

String agf = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";

现在我想把 this 的值放到键值对中的 hashmap 中。

以下是我正在使用但无法将其转换为哈希图的代码。请指导。

String agf = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";

String []tmp = StringUtils.split(agf,'*');
for (String v : tmp) {
    String[] t = StringUtils.split(v,'$');
    map.put(t[0], t[1]);
} 

【问题讨论】:

  • 将参数中的*$ 切换为split,然后就可以工作了。
  • 您确定给定键只有 1 个值吗?

标签: java string hashmap


【解决方案1】:
import java.util.HashMap;
import java.util.Map;

public class Parse{
    public static void main(String ...args){
        String agf = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";
        String [] split = agf.split("\\$");
        Map<String,String> map = new HashMap<String,String>();
        for(String temp : split){
            String [] tempo = temp.split("\\*");
            map.put(tempo[0],tempo[1]);
        }
        for(String mapkeys : map.keySet()){
            System.out.println(mapkeys+" ::: "+map.get(mapkeys));
        }
    }
}

如果给定键有多个值,请使用:

public static void main(String ...args){
        String agf = "abc*pqr*gas$sfd*ghn$atr*mnb$tre*fgt";
        String [] split = agf.split("\\$");
        Map<String,String> map = new HashMap<String,String>();
        for(String temp : split){
            String [] tempo = temp.split("\\*");
            StringJoiner sj = new StringJoiner(",");
            for(int i = 1; i < tempo.length;i++){
                sj.add(tempo[i]);
            }
            String value = sj.toString();
            map.put(tempo[0],value);
        }
        for(String mapkeys : map.keySet()){
            System.out.println(mapkeys+" ::: "+map.get(mapkeys));
        }
    }

希望对您有所帮助

【讨论】:

  • 如果给定键有多个值,请告诉我。
【解决方案2】:

查看您的示例字符串,您应该首先拆分 $(以获取单独的键值对),然后拆分 *(以分隔键和值)

【讨论】:

    【解决方案3】:

    切换“*”和“$”。

    String agf = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";
    String []tmp = StringUtils.split(agf,'$');
    for (String v : tmp) {
       String[] t = StringUtils.split(v,'*');
       map.put(t[0], t[1]);
    } 
    

    【讨论】:

      【解决方案4】:

      使用string.split("//$")string.split("//*") 作为您的用例的正则表达式。

      完整代码:

              String str = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";
              String[] entries = str.split("\\$");
      
              Map<String, String> map = new HashMap<String, String>();
      
              for (int i=0; i<entries.length; i++) {
                  String[] entry = entries[i].split("\\*");
                  map.put(entry[0], entry[1]);
              }
      
              System.out.println("Entries are -> " + map.entrySet());
      

      【讨论】:

        【解决方案5】:
        String agf = "abc*pqr$sfd*ghn$atr*mnb$tre*fgt";
        
        \\next array will contain such elements: "abc*pqr", "sfd*ghn", "atr*mnb", "tre*fgt"
        String[] pairs = agf.split("\\$");
        
        Map<String, String> map = new HashMap();
        
        for (String pair: pairs) {
        
            \\in first iteration next array will contain such elements: "abc", "pqr"
            String[] t = pair.split("\\*");
        
            \\just fill out map with data
            map.put(t[0], t[1]);
        }
        

        【讨论】:

        • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of howwhy 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
        • @nithesh-kumar 这是有效的,当我在做 System.out.println(map);我有这样的输出: {sfd=ghn, abc=pqr, tre=fgt, atr=mnb}
        猜你喜欢
        • 2019-08-26
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 2016-10-21
        • 1970-01-01
        • 1970-01-01
        • 2017-06-02
        • 2010-11-05
        相关资源
        最近更新 更多