java 如何在Java中将String转换为HashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25132623/
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
How to convert String to HashMap in Java
提问by Karthick
i am new to Java. i want to convert string to HashMap
. but i dont know how to convert it. below is my code.
我是 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);
}
}
my input :
我的输入:
“u1”,“u10”
“u2”,“u41”
“u3”,“u10”
“u4”,“u81”
“u5”,“u10”
“u6”,“u10”
“u7”,“u31”
“u8”,“u11”
my output of string("v value") :
我的 string("v value") 输出:
“u1” “u10” “u2” “u41” “u3” “u10” “u4” “u81” “u5” “u10” “u6” “u10” “u7” “u31” “u8” “u11”
“u1” “u10” “u2” “u41” “u3” “u10” “u4” “u81” “u5” “u10” “u6” “u10” “u7” “u31” “u8” “u11”
i need to convert this string(v Value)
into HashMap<string,String
>(like key,value pair).how to do that?
can anyone help to find solution please?
我需要将其转换string(v Value)
为HashMap<string,String
>(如键、值对)。怎么做?
任何人都可以帮助找到解决方案吗?
回答by Vimal Bera
At the time of reading put the values in Map instead of storing it in String variable.
在读取时将值放在 Map 中,而不是将其存储在 String 变量中。
Try this way :
试试这个方法:
while((ln=fl.readLine())!=null){
String[] pair = ln.split(" ");
map.put(pair[0],pair[1]);
}
回答by Jens
Split your line on "," and add put the key and value to the map:
在“,”上拆分您的行,并将键和值添加到地图中:
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();
}
回答by Navneet Boghani
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]));
}
回答by user3905989
Try this line it will be usefull for you
试试这条线它对你有用
Map<String,String> lists=new HashMap<String,String>();
int i=1;
for(String s1:str){
lists.put(String.valueof(i),s1);
i++;
}
You can easily access from index.
您可以轻松地从索引访问。
回答by Rahul Jain
Add map.put(s,v); in for loop where you are print s,v;
添加 map.put(s,v); 在 for 循环中打印 s,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);
}
回答by Anup Ash
Try this
试试这个
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;
}