Search code examples
javaandroidloopsgenericsandroid-room

How can I sum the data in the nested Room database?


This will probably be the simplest question on SO so first of all I apologize but I couldn't establish the answer so I had to ask it here.

Suppose you have a flowing list like this. I want to sum the dayId0 values, then dayId1 and print them on the upper layer in Room. For example, dayId0 will continue as food_kcal_sum=350 and dayId1 food_kcal_sum=45. How should I create a loop for this?

[
   {
       "dayId": 0,
       "food_kcal": 270.0,
       "id": 1
   },
   {
       "dayId": 0,
       "food_kcal": 60.0,
       "id": 2
   },
   {
       "dayId": 0,
       "food_kcal": 20.0,
       "id": 3
   },
   {
       "dayId": 1,
       "food_kcal": 15.0,
       "id": 4
   },
   {
       "dayId": 1,
       "food_kcal": 30.0,
       "id": 5
   }
]

Solution

  • I made an attempt using Gson.

    public class DriverClass {
    
    Map<Integer, Integer> mapValues = new HashMap<>();
    
    public static void main(String[] args) {
    
        Map<Integer, Integer> map = new DriverClass().sumValues();
        map.entrySet().forEach(i -> System.out.println(i.getKey() + " " + i.getValue()));
    
    }
    
    private Map<Integer, Integer> sumValues() {
        Gson gson = new Gson();
        JsonArray jsonArray = gson.fromJson(myJson(), JsonArray.class);
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
            JsonElement jsonElementDayId = jsonObject.get("dayId");
            JsonElement jsonElementFoodCal = jsonObject.get("food_kcal");
            calculateValues(jsonElementDayId.getAsInt(), jsonElementFoodCal.getAsInt());
    
        }
        return mapValues;
    }
    
    private void calculateValues(int jsonElementDayId, int jsonElementFoodCal) {
        if (mapValues.containsKey(jsonElementDayId)) {
            mapValues.put(jsonElementDayId, mapValues.get(jsonElementDayId) + jsonElementFoodCal);
        } else {
            mapValues.put(jsonElementDayId, jsonElementFoodCal);
        }
    }
    
    public static String myJson() {
        return """
                [
                    {
                        "dayId": 0,
                            "food_kcal": 270.0,
                            "id": 1
                    },
                    {
                        "dayId": 0,
                            "food_kcal": 60.0,
                            "id": 2
                    },
                    {
                        "dayId": 0,
                            "food_kcal": 20.0,
                            "id": 3
                    },
                    {
                        "dayId": 1,
                            "food_kcal": 15.0,
                            "id": 4
                    },
                    {
                        "dayId": 1,
                            "food_kcal": 30.0,
                            "id": 5
                    }
                ]
            """;
    }
    

    }

    outputs:

    0 350
    1 45