如何在Java中将String转换为HashMap

时间:2014-08-05 06:42:05

标签: java string hashmap

我是Java的新手。我想将字符串转换为HashMap。但我不知道如何转换它。下面是我的代码。

public class Excer5sam {     
    public static void main(String[] args) throws IOException{    
        BufferedReader fl=new BufferedReader(new FileReader("/home/mansoor/Documents/Fileip.txt"));

        Map<String, String>map=new HashMap<String, String>();   
        List<String>str=new ArrayList<>();   
        String ln=null;  
        while((ln=fl.readLine())!=null){  
            str.add(ln);   
            }   
        fl.close();  
        String s="";   
        for(String s1:str){   
            s+=s1+",";   
            }   
        System.out.println("value of s:"+s);   
        String v=s.replace(",", " ");  
        System.out.println("v value:"+"  "+v);  
        }  
}

我的意见:

“u1”,“u10”  
“u2”,“u41”  
“u3”,“u10”  
“u4”,“u81”  
“u5”,“u10”  
“u6”,“u10”  
“u7”,“u31”  
“u8”,“u11”  

我的字符串输出(“v value”):

  

“u1”“u10”“u2”“u41”“u3”“u10”“u4”“u81”“u5”“u10”“u6”“u10”   “u7”“u31”“u8”“u11”

我需要将此string(v Value)转换为HashMap<string,String&gt;(如键,值对)。如何做到这一点?
有人可以帮忙找到解决方案吗?

6 个答案:

答案 0 :(得分:4)

在阅读时将值放在Map中,而不是将其存储在String变量中。

尝试这种方式:

while((ln=fl.readLine())!=null){  
    String[] pair = ln.split(" ");
    map.put(pair[0],pair[1]);
} 

答案 1 :(得分:2)

将您的行拆分为“,”并将键和值添加到地图中:

public static void main(String[] args) throws IOException{    
    BufferedReader fl=new BufferedReader(new FileReader("T:/temp/Fileip.txt"));

    Map<String, String>map=new HashMap<String, String>();   
    List<String>str=new ArrayList<String>();   
    String ln=null;  
    while((ln=fl.readLine())!=null){  
        String[] temp = ln.split(",");
        map.put(temp[0], temp[1]);
        }   
    fl.close();  
    }  

答案 2 :(得分:1)

     HashMap<String, String> myMap = new HashMap<String, String>();


         String s = "SALES:0,SALE_PRODUCTS:1,EXPENSES:2,EXPENSES_ITEMS:3";
         String[] pairs = s.split(",");
         for (int i=0;i<pairs.length;i++) {
             String pair = pairs[i];
             String[] keyValue = pair.split(":");
             myMap.put(keyValue[0], (keyValue[1]));
         }

答案 3 :(得分:0)

试试这一行对你有用

 Map<String,String> lists=new HashMap<String,String>();
  int i=1;
 for(String s1:str){   
       lists.put(String.valueof(i),s1);
     i++;
        }  

您可以从索引轻松访问。

答案 4 :(得分:0)

添加map.put(s,v);在你打印的for循环中,v;

for(String s1:str){   
            s+=s1+",";   
            }   
        System.out.println("value of s:"+s);   
        String v=s.replace(",", " ");  
        System.out.println("v value:"+"  "+v);
        map.put(s,v); 
        }

答案 5 :(得分:0)

试试这个

private static Map<String, String> splitToMap(String in) {
     String value = StringUtils.substringBetween(in, "{", "}"); //remove curly brackets
     String[] keyValuePairs = value.split(",");              //split the string to creat key-value pairs
     Map<String,String> map = new HashMap<>();               

     for(String pair : keyValuePairs)                        //iterate over the pais
     {
         String[] entry = pair.split(":");                   //split the pairs to get key and value 
         map.put(entry[0].trim().replaceAll( "[\"]", ""), entry[1].trim().replaceAll( "[\"]", ""));          //add them to the hashmap
     }
        return map;
    }