Sobes.tech
Junior — Middle+

Converting a string into a set of key-value pairs

livecode

Task condition

This is a string containing multiple pairs of "key=value" separated by the ; symbol. You need to implement a class ParseHolder that provides:

  1. void parse(String str) — parsing the input text and filling the internal data structure.
  2. String getValue(String key) — instant (O(1)) access to the value by the specified key.
  3. String generateRecord() — forming the original string from the stored pairs.

Example string:

key1=val1;key1=abc;key2=val2;key3=val3;key1=val4;key2=val5;key1=val6
public class ParseHolder {

    void parse(String str) {
        // ...
    }

    String generateRecord() {
        return null;
    }

    public String getValue(String key) {
        return null;
    }

    public static void main(String[] args) throws Exception {
        String testString = "key1=val1;key1=abc;key2=val2;key3=val3;" +
                            "key1=val4;key2=val5;key1=val6";

        ParseHolder parser = new ParseHolder();
        parser.parse(testString);
        System.out.println(parser.generateRecord());
    }
}