1

I have a string where i have multiple values. The key and value are separated by * and the whole value is separated by $.

Below is the example:

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

And now I want to put the value of this into hashmap in key value pair.

Below is the code i was using but didnt work for converting it into hashmap. Please guide.

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]);
} 
2
  • 4
    Switch * and $ in the arguments to split, then it's going to work. Commented Feb 15, 2017 at 13:52
  • are you sure you had only 1 value for a given key? Commented Feb 15, 2017 at 14:00

5 Answers 5

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

if you had multiple values for a given key then use this:

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

Hope you found this helpful

Sign up to request clarification or add additional context in comments.

1 Comment

If you had multiple values for given key please do let me know.
1

Looking at your example string, you should be splitting first on $ (to get individual key value pairs) and then on * (to separate key and values)

Comments

0

Switch '*' and '$'.

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

Comments

0

Use string.split("//$") and string.split("//*") as regex for your use case.

Full code :

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

Comments

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

2 Comments

Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how and why this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
@nithesh-kumar This is working, when I'm doing System.out.println(map); I have such output: {sfd=ghn, abc=pqr, tre=fgt, atr=mnb}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.