Home > Net >  Adding List of JSONArray value into List of List and Put List of List One by one Map
Adding List of JSONArray value into List of List and Put List of List One by one Map

Time:12-02

1st Step : adding value[x,y,z] into the List roles. Then Put key and roles(value) into KV(HashMap).

|
|___>>>>{a=[x, y, z]}  

2nd Step: adding value[x,z] into the roles List its added to the List successfully but while adding it into roles it changes the value[x,y,z] to value[x,z] in 1st Step.

|
|_____>>>>{a=[x, z], b=[x, z]}  

Expected: {a=[x, y, z],b=[x, z]}

3nd Step: adding value[x,y,z] into the roles List its added to the List successfully but while adding it into roles it changes the value[x,z] to value[x,y,z] in 2st Step and for that changed in 1st step during performing 2nd step.

|
|______>>>>{a=[x, y, z], b=[x, y, z], c=[x, y, z]}    

Expected: {a=[x, y, z],b=[x, z],c=[x, y, z]}

Please help me with this code, why its Changing the Values that I previously added to Map.

"json":{
"a": [
  "x",
  "y",
  "z"
],
"b": [
  "x",
  "z"
],
"c": [
  "x",
  "y",
  "z"
],
"d": [
  "y",
  "z"
]
}
public Map<String,List<String>> getUserDetails(String json) throws IOException
{
    Map<String,List<String>> **KV** = new HashMap<>();
    List<String> **roles** = new LinkedList<>();


    List<String> arrayKeys = jsonUtil.getJsonArrayKey(json);
    System.out.println("Array Key      :  "   arrayKeys);
    for (String **key** : arrayKeys)
    {
        roles.clear();
        JSONObject jsonObject = new JSONObject(json);
        JSONArray explrObject = jsonObject.getJSONArray(key);
        for (int i = 0; i < explrObject.length(); i  )
        {
            String value = (explrObject.get(i).toString());
            System.out.println("Array Value : "   value);
            **roles**.add(value);
        }
         **KV**.put(**key**,**roles**);
        System.out.println("Key and Value     :" KV);
    }
    return profileOrg_KV;
}



Actual OutPut:

Array Key      :  [a, b, c, d]

Array Value : x
Array Value : y
Array Value : z
Key and Value      :{a=[x, y, z]}

Array Value : x
Array Value : z
Key and Value      :{a=[x, z], b=[x, z]}

Array Value : x
Array Value :y
Array Value : z
Key and Value      :{a=[x, y, z], b=[x, y, z], c=[x, y, z]}

Array Value : y
Array Value : z
Key and Value     :{a=[x, y, z], b=[x, y, z], c=[y , z], d=[x, y, z]}

Expected OutPut:

Array Key      :  [a, b, c, d]

Array Value : x
Array Value : y
Array Value : z
Key and Value      :{a=[x, y, z]}

Array Value : x
Array Value : z
Key and Value      :{a=x, y, z], b=[x, z]}

Array Value : x
Array Value :y
Array Value : z
Key and Value      :{a=[x, y, z], b=[x, z], c=[x, y, z]}

Array Value : y
Array Value : z
Key and Value     :{a=[x, y, z], b=[x, z], c=[y , z], d=[x, z]}

CodePudding user response:

You're associating the same the list roles with every Key in your Map (because you're using a reference to the same object, and invoking clean() on the list only removes all its contents it doesn't allocate a new list as you might be expected).

You need to instantiate a new list with each iteration step, so that every Key would be mapped to its own List which is not shared with other Keys.

public Map<String, List<String>> getUserDetails(String json) throws IOException {
    Map<String, List<String>> rolesByKey = new HashMap<>();
    
    List<String> arrayKeys = jsonUtil.getJsonArrayKey(json);
    
    for (String key : arrayKeys) {
        List<String> roles = new LinkedList<>();
        JSONObject jsonObject = new JSONObject(json);
        JSONArray explrObject = jsonObject.getJSONArray(key);
        for (var role : explrObject) {
            roles.add(role.toString());
        }
        rolesByKey.put(key, roles);
    }
    return rolesByKey;
}

Sidenote: please, get familiar and try to adhere to the Java language naming conventions.

  • Related