0

After splitting the below String separated with "/", in each string 1st 4 character is key of a map and rest is value, for example in "GOMN1Q", GOMN is key and 1Q is value, likewise in "SNIF4A25E8", SNIF is key and 4A25E8 is value.

/GOMN1Q/SNIF4A25E8/BOOKTQNN013A/RATE109.00/ROOMMSG1/RD1MSG11/RD2MSG12/CMUY/SMKN/BOOKTQNNN5GM/RATE114.00/ROOMMSG2/RD1MSG21/RD2MSG22/CMUY/CMUY/SMKN/BOOKTQNNB1KQ/RATE119.00/ROOMMSG3/RD1MSG31/RD2MSG32/CMUY/CMUY/SMKN

which is in below form,each

[GOMN:1Q
SNIF:4A25E8
k3: [BOOK:TQNN013A RATE:109.00 k4:[MSG1, MSG11, MSG12] CMUY:11 SMOK:N]
    [BOOK:TQNNN5GM RATE:114.00 k4:[MSG2, MSG21, MSG22] CMUY:22 SMOK:N]
    [BOOK:TQNNB1KQ RATE:119.00 k4:[MSG3, MSG31, MSG32] CMUY:33 SMOK:N]]

I am using map as LinkedHashMap as LinkedHashMap allow Null value and maintaining the insertion order. Please advise how to store after splitting the slash separated string. Thank you.

  • 3
    You'd better create you own class to store this kind of specific object, 2 Strings and a List of ... (or custom class again) – azro Mar 23 '18 at 07:44
  • Thanks Azro, Any suggestion, how to slice data between two Book key , e.g., /BOOKTQNN013A....../BOOKTQNNN5GM – The Learner Mar 23 '18 at 07:53

1 Answers1

0

Actually to store the data as maps with lists is not the best option - it is better in most cases to create your own domain classes which represent the actual business model.

Yet if you really want to store the data that way you can do it.

Here is a simple example on how you can re-create the example given in the question using maps and lists without creating classes to represent your business model objects:

public static void main(String[] args) throws IOException {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("GOMN", 10);
    map.put("SNIF", "4A25E8");
    List<Map<String, Object>> k3List = new ArrayList<>();
    createK3Map(k3List, "TQNN013A", 109.00, Arrays.asList("MSG1", "MSG11", "MSG12"), 11, "N");
    createK3Map(k3List, "TQNNN5GM", 114.00, Arrays.asList("MSG2", "MSG21", "MSG22"), 22, "N");
    map.put("k3", k3List);
    System.out.println(map);
}

private static void createK3Map(List<Map<String, Object>> k3List, String book, double rate, List<String> messages, int cmuy, String smok) {
    Map<String, Object> k3_1 = new LinkedHashMap<>();
    k3_1.put("BOOK", book);
    k3_1.put("RATE", rate);
    k3_1.put("k4", messages);
    k3_1.put("CMUY", cmuy);
    k3_1.put("SMOK", smok);
    k3List.add(k3_1);
}

This prints out:

{GOMN=10, SNIF=4A25E8, k3=[{BOOK=TQNN013A, RATE=109.0, k4=[MSG1, MSG11, MSG12], CMUY=11, SMOK=N}, {BOOK=TQNNN5GM, RATE=114.0, k4=[MSG2, MSG21, MSG22], CMUY=22, SMOK=N}]}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • Thanks Fernandes for help. I would go with your second option by creating own domain classes. Can you please advise how to create Map with k3 as key and value as List> by reading the splitted strings. – The Learner Mar 23 '18 at 09:25