Get latest jsons with respect to [version and name] key in subjson in java This is the json

[
  {
    "SUBTASK": {
      "id": 10,
      "name": "TestMap",
      "description": "",
      "version":"1.0"
    }
  },
  {
    "SUBTASK": {
      "id": 23,
      "name": "TestMap",
      "description": "",
      "version":"2.0"
    }
  },
  {
    "SUBTASK": {
      "id": 12,
      "name": "Tree",
      "description": "",
      "version":"13.0"
    }
  },
  {
    "SUBTASK": {
      "id": 32,
      "name": "Tree",
      "description": "",
      "version":"10.0"
    }
  }
]


Need to get latest among the number of SUBTASKS ie. filter the latest versions of SUBTASK with respected to name.

So the final json will be like:

final:[{"SUBTASK":{"id":12.0,"name":"Tree","description":"","version":"13.0"}},{"SUBTASK":{"id":23.0,"name":"TestMap","description":"","version":"2.0"}}]


I achived using the below the java code :

Java Snippet

 public void getLatest(String json) throws Exception {
        // converting json to java Object
        List&lt;Map&lt;String, Object&gt;&gt; taskList = new Gson().fromJson(json, new TypeToken&lt;List&lt;Map&lt;String, Object&gt;&gt;&gt;() {
        }.getType());

        Map&lt;String, Object&gt; taskVersionedMap = new HashMap&lt;String, Object&gt;();

        for (int i = 0; i &lt; taskList.size(); i++) {
            Map&lt;String, Object&gt; processMap = taskList.get(i);
            Map&lt;String, Object&gt; process = (Map&lt;String, Object&gt;) processMap.get("SUBTASK");

            String process_name = process.get("name").toString() + "_" + process.get("version");
            taskVersionedMap.put(process_name, processMap);

            for (Map.Entry&lt;String, Object&gt; entry : taskVersionedMap.entrySet()) {
                String[] split = entry.getKey().split("_");
                double version = Double.parseDouble(split[1]);
                String name = split[0];
                for (Map.Entry&lt;String, Object&gt; entry2 : taskVersionedMap.entrySet()) {
                    String[] split_2 = entry2.getKey().split("_");
                    double version_2 = Double.parseDouble(split_2[1]);
                    String name_2 = split_2[0];
                    if (name.equals(name_2)) {
                        if (version &lt; version_2) {
                            String removedProcessName = name + "_" + version;
                            taskVersionedMap.put(removedProcessName, "ignored");
                        }
                    }
                }

            }
        }
        List&lt;Map&lt;String, Object&gt;&gt; filteredVersionList = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
        for (Map.Entry&lt;String, Object&gt; mapValues : taskVersionedMap.entrySet()) {
            if (!mapValues.getValue().toString().equals("ignored")) {
                try {
                    filteredVersionList.add((Map&lt;String, Object&gt;) mapValues.getValue());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("final:"+new Gson().toJson(filteredVersionList));

    }


The above code gives accurate result expected and is time consuming.

But is it possible to optimize the code since real data json at process will be verry big almost 500 more subtasks
First of all, you have a grouping operation by process name, then you have the task of finding the maximum by version number per group.

Thus, instead of using nested loops and somehow mashing it all together, simply perform those operations.

As standard operations like grouping and finding a maximum are alreay presenet in the java stream api, all we need is a few helpers to extract the name and the version:

Function&lt;Map&lt;String, Object&gt;, String&gt; extractName = m -&gt; (String) ((Map&lt;String, Object&gt;) m.get("SUBTASK")).get("name");
ToDoubleFunction&lt;Map&lt;String, Object&gt;&gt; extractVersion = m -&gt; Double.parseDouble((String) ((Map&lt;String, Object&gt;) m.get("SUBTASK")).get("version"));


Then, first group by name:

Map&lt;String, List&lt;Map&lt;String, Object&gt;&gt;&gt; byName = taskList.stream()
    .collect(Collectors.groupingBy(extractName));


... which gives you a list of each task type in the value lists.

Now, we can disregard the grouping keys and extract the maximum per list:

List&lt;Map&lt;String, Object&gt;&gt; result = byName.values().stream()
    .map(l -&gt; l.stream().max(Comparator.comparingDouble(extractVersion)))
    .map(Optional::get)
    .collect(Collectors.toList());


Note: the stream.max returns an optional, which would be unset for an empty list. As we cannot generate empty lists here, it is save to simply map via Optional::get to retrieve the value.

All is left is to encode the result list back to json, e.g.

String jsonResult = new GsonBuilder().setPrettyPrinting().create().toJson(result);


Note however, that you should better use a method which operates directly on the data structures, i.e. input and output are List&lt;Map&lt;String, Object&gt;&gt; and another outer wrapper method to do the decoding and encoding. Do one thing in a method.