1

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?

6 Answers 6

4

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]);
} 
Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much. its working. if i print map,it produced the following output, Map:{“u8”=“u11”, “u7”=“u31”, “u1”=“u10”, “u4”=“u81”, “u3”=“u10”, “u5”=“u10”, “u6”=“u10”, “u2”=“u41”} . But if i checked all key and value by using map.containsValue("u10") like that it print "FALSE" as output. i cant use this map for anymore. can you help me to solve this please?
Are you sure, your map stores all these values in Key-Value pair?
ya now its working correctly after change the split(","), and input format like 'u1'='u10'. thank you so much..
2

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

1 Comment

If there is a comma in String like "name : Name, MidName" then it will got problem.
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]));
         }

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

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.

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.