0

I want to split below string

G04:AMPARAMS|DCode=50|XSize=66mil|YSize=66mil|CornerRadius=0mil|HoleSize=0mil|Usage=FLASHONLY|Rotation=0.000|XOffset=0mil|YOffset=0mil|HoleType=Round|Shape=Octagon|*

I was first spliting the code from '|' pipe sign then spliting again from '=' equal sign but problem i am facing here is that how to store the value in hashmap as they are in a loop so i can't store it in a hashmap. Any Possible solution would be appricated.

String[] temp=line.split("\\|");
for(String p:temp){
    HashMap<String,String>attributes=new HashMap<String,String>();
    String[] key =p.split("\\=");

    for(String tmp:key){
    //System.out.println(tmp);
    attributes.put();
    }

3 Answers 3

4

No Streams:

public static Map<String, String> split(String str) {
    final Pattern sep = Pattern.compile("\\s*\\|\\s*");
    final Pattern eqSep = Pattern.compile("(?<key>[^=\\s]+)\\s*=\\s*(?<value>[^=]+)");

    Map<String, String> map = new HashMap<>();

    for (String part : sep.split(str)) {
        Matcher matcher = eqSep.matcher(part);

        if (matcher.matches())
            map.put(matcher.group("key"), matcher.group("value"));
    }

    return map;
}

With Streams:

public static Map<String, String> split(String str) {
    final Pattern sep = Pattern.compile("\\s*\\|\\s*");
    final Pattern eqSep = Pattern.compile("(?<key>[^=\\s]+)\\s*=\\s*(?<value>[^=]+)");

    return sep.splitAsStream(str)
              .map(eqSep::matcher)
              .filter(Matcher::matches)
              .collect(Collectors.toMap(matcher -> matcher.group("key"), matcher -> matcher.group("value")));
}
Sign up to request clarification or add additional context in comments.

Comments

3

This is easier done via streams:

Arrays.stream(input.split("\\|"))
      .map(s -> s.split("="))
      .filter(a -> a.length == 2)
      .collect(toMap(a -> a[0], a -> a[1], (l, r) -> r, HashMap::new));
  • Arrays.stream create a Stream<String> which we can then further refine our query via several built-in stream API methods.
  • map splits each string on "=" therefore returns a Stream<String[]>
  • filter retains the array elements that have exactly two elements
  • finally, we supply a toMap collector to the collect method to build the Map<String, String>

2 Comments

It may be worth adding that the OP specifically asked for a HashMap, but toMap creates a map of unspecified nature.
to avoid creating an intermediate array it is better to use Pattern.compile.splitAsStream which returns stream directly: Pattern.complile("\\|").splitAsStream(input)
1

Looks like what you need is:

String[] temp = line.split("\\|");
Map<String,String> attributes = new HashMap<String,String>();
for (String p : temp) {
    String[] key = p.split("\\=");
    if (key.length == 2) {
        attributes.put(key[0],key[1]);
    }
}

Note that you should create a single HashMap outside the loop, since you want to store all the key-value pairs in the same HashMap.

Comments

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.