Karthick
Karthick

Reputation: 97

How to convert String to HashMap in Java

i am new to Java. i want to convert string to HashMap. but i dont know how to convert it. below is my code.

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") :

“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?

Upvotes: 1

Views: 13274

Answers (6)

Navneet Boghani
Navneet Boghani

Reputation: 17

     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]));
         }

Upvotes: 1

Anup Ash
Anup Ash

Reputation: 964

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;
    }

Upvotes: 0

Rahul Jain
Rahul Jain

Reputation: 115

Add map.put(s,v); in for loop where you are print 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); 
        }

Upvotes: 0

user3905989
user3905989

Reputation: 1

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.

Upvotes: 0

Jens
Jens

Reputation: 69440

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();  
    }  

Upvotes: 2

Vimal Bera
Vimal Bera

Reputation: 10497

At the time of reading put the values in Map instead of storing it in String variable.

Try this way :

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

Upvotes: 4

Related Questions