0

I have a json array list in below format in same order. I cannot expect the data in sorting , it may vary, Company-x may come in any order.

Company-1 - {Branch-1, Branch-4, Branch-8}
Company-2 - {Branch-9, Branch-11, Branch-22}
Company-4 - {Branch-45, Branch-64, Branch-18}
Company-1 - {Branch-10, Branch-43, Branch-82}
Company-2 - {Branch-15, Branch-44, Branch-86}

Also,this may increase more than 10,000 rows. I need to make a key value pair without affecting the performance. I need to store above data in HashMap. (Map < String, List < Branch > ).

For example I need result as below format :

  [Company-1,{Branch-1, Branch-4, Branch-8,Branch-10, Branch-43, Branch-82}]
    [Company-2,{Branch-9, Branch-11, Branch-22,Branch-15, Branch-44, Branch-86}]
    [Company-4,{Branch-45, Branch-64, Branch-18}]
....etc.

How to achieve this in java ?

Here is the json format. Key is CID and Values are (Branch_ID and Names)

[
            {

                "CID": "4545",
                "BRANCH_ID": "0041",
                "Name": "BID41",


            },
            {

                "ID": "4546",
                "BRANCH_ID": "0051",
                "Name": "BID51",


            },
            {

                "ID": "4545",
                "BRANCH_ID": "0042",
                "Name": "BID42",


            },
            {

                "ID": "4546",
                "BRANCH_ID": "0052",
                "Name": "BID52",


            },
            {

                "ID": "4545",
                "BRANCH_ID": "0043",
                "Name": "BID43",


            },
            {

                "ID": "4545",
                "BRANCH_ID": "0053",
                "Name": "BID53",


            }
        ]
WhoAmI
  • 57
  • 7
  • What do you mean "without affecting the performance"? – teolandon Mar 06 '18 at 19:50
  • What code have you written to try and solve your problem? What JSON parsing tool are you using? Your pasted examples are not valid JSON, so what does your actual JSON look like? What data structures/POJOs do you already have? The information you've given us isn't enough to help you. – Brian Mar 06 '18 at 19:51
  • I am trying to achieve for loop but got confused to push the values into the existing key in Map. Also I dont have an issue with JSON, it is a valid JSON only. – WhoAmI Mar 06 '18 at 19:54
  • @teolandon , I am expecting huge data. so it should not take more time for iteration while converting as Map. – WhoAmI Mar 06 '18 at 19:55
  • Couldn't you just do one linear pass of the list and add any key you haven't seen before, with the branches it has, and if a key already exists, just add the new branches to the key's value? Or am I missing something? – teolandon Mar 06 '18 at 19:57
  • @teolandon, I am not good at collection algorithm . Can you pls give the explanation with example ? – WhoAmI Mar 06 '18 at 20:00
  • what's wrong with using a HashMap>?? – RAZ_Muh_Taz Mar 06 '18 at 20:01
  • @RAZ_Muh_Taz, I can store the key and Branches but if again the same key exist , then how to push the values to the old one? – WhoAmI Mar 06 '18 at 20:03
  • 2
    call myHashmap.get(existingKey).add(newBranch), if the key already exists then use the value stored and simply append to the existing set. – RAZ_Muh_Taz Mar 06 '18 at 20:06

1 Answers1

0

If you are trying to convert Json data to Map < String, List > , here is the solution:

  public Map<String , List<String>> convertMapList(String jsonStr) {
    Type mapType = new TypeToken<Map<String, Map>>(){}.getType();  
   Map<String, List<String>> son = new Gson().fromJson(jsonStr, mapType);
        return son;
    }   

For this, you have to add Gson library in your project

Mamun Kayum
  • 161
  • 7