I have an entity class:
public class ChunkItems {
private String itemId;
private String status;
//Five more other fields.
}
I'm getting a json response like this:
{
"chunkItemStatus": [
{"101-a":"SUCCESS"},
{"102-b":"FAIL"},
{"103-c":"SUCCESS"}
]
}
In the above JSON array, each JSON object key represents the itemId
of ChunkItems
and the value represents the status
of ChunkItems
.
I have a list of ChunkItems
where the status
field is not valued. I need to update it from this JSON array.
Below is what I have tried which is working fine:
void updateStatus(List<ChunkItems> items){
JSONArray chunkItemResponse = chunkResponse.getJSONArray("chunkItemStatus");
chunkItemResponse.forEach(
chunkItem -> {
String key = ((JSONObject) chunkItem).keys().next();
String value = ((JSONObject) chunkItem).getString(key);
ChunkItems obj =
items
.stream()
.filter(item -> key.equals(item.getItemId()))
.findAny()
.orElseThrow(
() ->
new NoSuchElementException(
"Item " + key + " is not present in the chunk: "+ items));
obj.setStatus(value);
}
Is there any way where these values can be mapped to the list without iterating or may reduce the complexity?
Aucun commentaire:
Enregistrer un commentaire