Home > database >  Creating map from a string based on the keys provided but the string doesn't have any delimiter
Creating map from a string based on the keys provided but the string doesn't have any delimiter

Time:06-23

Provided java String

String s = "DIMENSION 24cm 34cm 12cm DETAILED SPECIFICATION Twin/Twin XL Flat Sheet: 105"l x 74"w. CARE For best results, machine wash warm with like colors. COLOURS red blue green"; 

Keys are = DIMENSIONS, DETAILED SPECIFICATION, CARE, COLOURS

We need to create Map<String, String> where keys will be as provided above and corresponding text will be the value.

Map will be something like DIMENSION, 24cm 34cm 12cm DETAILED SPECIFICATION, Twin/Twin XL Flat Sheet: 105"l x 74"w. CARE, For best results, machine wash warm with like colors. COLOURS, red blue green";

And not necessary that all they keys and values are present in the string.

As the string doesn't have delimiter i am unable create a map from it

With plane java i am able to do like this

    if(s.contains("ASSEMBLY")) {
        ass = s.substring(s.indexOf("COLOURS")   8);
        s = s.replaceAll(s.substring(s.indexOf("COLOURS")),"");
    }
    if(s.contains("OVERALL")){
        ov = s.substring(s.indexOf("CARE")   5);
        s = s.replaceAll(s.substring(s.indexOf("CARE")),"");
    }
    if(s.contains("CARE")){
        care1 = s.substring(s.indexOf("DETAILED SPECIFICATION")   24);
        s = s.replaceAll(s.substring(s.indexOf("DETAILED SPECIFICATION")),"");
    }
    if(s.contains("DIMENSIONS")){
        de1 = s.substring(s.indexOf("DIMENSIONS")   11);
        s =s.replaceAll(s.substring(s.indexOf("DIMENSIONS")),"");
    }

If we have delimiter then i am able to do it like this.

Map<String, String> map = Stream.of(s)
.map(s -> s.split("="))
.collect(Collectors.toMap(s -> s[0], s -> s[1]));

CodePudding user response:

Regex approach:

    String s = "DIMENSION 24cm 34cm 12cm DETAILED SPECIFICATION Twin/Twin XL Flat Sheet: 105 w. COLOURS red blue green";
    String[] keys = new String[]{"DIMENSION", "DETAILED SPECIFICATION", "CARE", "COLOURS"};
    String keyRegex = String.join("|", keys);
    Pattern p = Pattern.compile("(?<key>"   keyRegex   ") (?<value>((?!("   keyRegex   ")).)*)");
    Matcher m = p.matcher(s);

    Map<String, String> result = new HashMap<>();
    while (m.find()) {
        result.put(m.group("key"), m.group("value"));
    }

CodePudding user response:

This might help you:

String str = "DIMENSION 24cm 34cm 12cm DETAILED SPECIFICATION Twin/Twin XL Flat Sheet: 105 w. CARE For best results, machine wash warm with like colors. COLOURS red blue green";

    String[] keys = {"DIMENSIONS", "DETAILED SPECIFICATION", "CARE", "COLOURS"};

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

    for (int index = 0; index < keys.length; index  ) {

        String subString = "";
        if(index   1 < keys.length) {
            int start = str.indexOf(keys[index]   " ") > 0 ? str.indexOf(keys[index]   " ")   keys[index].length() : str.indexOf(keys[index]);
            int end = str.indexOf(keys[index   1]);
            if(start < 0 || end < 0) {
                continue;
            }
            subString = str.substring(start, end);
        }  else {
            subString = str.substring(str.indexOf(keys[index]   " "));
        }
        map.put(keys[index], subString);
    }
    System.out.println(map);
  • Related