如何对嵌套 Room 数据库中的数据求和?
How can I sum the data in the nested Room database?
这可能是关于 SO 的最简单的问题所以首先我很抱歉,但我无法确定答案所以我不得不在这里问。
假设您有一个这样的流动列表。我想对 dayId0 值求和,然后对 dayId1 求和并将它们打印在 Room 的上层。例如,dayId0 将继续为 food_kcal_sum=350 和 dayId1 food_kcal_sum=45。我应该如何为此创建一个循环?
[
{
"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
}
]
我尝试使用 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
}
]
""";
}
}
输出:
0 350
1 45
这可能是关于 SO 的最简单的问题所以首先我很抱歉,但我无法确定答案所以我不得不在这里问。
假设您有一个这样的流动列表。我想对 dayId0 值求和,然后对 dayId1 求和并将它们打印在 Room 的上层。例如,dayId0 将继续为 food_kcal_sum=350 和 dayId1 food_kcal_sum=45。我应该如何为此创建一个循环?
[
{
"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
}
]
我尝试使用 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
}
]
""";
}
}
输出:
0 350
1 45