英文:
How to one list Value to another list value in Java?
问题
我想在这里添加列表中的金额和值,比如一个列表的金额加上另一个列表的金额,一个列表的值加上另一个列表的值,使用Java程序,我该如何实现这个目标
输入JSON
{
"data": [
{
"amount": "100",
"value": "200"
},
{
"amount": "200",
"value": "200"
}
]
}
所需输出
"amount": 300
"value": 400
英文:
Here I want to add the amount & value which is coming in list such as amount of one list + amount of another list, value of one list + value of another list with Java program , How can I achieve this
Input JSON
{
"data": [
{
"amount": "100",
"value": "200",
},
{
"amount": "200",
"value": "200",
}
]
}
required output
"amount":300
"value":400
答案1
得分: 1
定义一个DTO如下所示:
class AmountInfo {
int value;
int amount;
}
使用以下代码获取所需内容:
int value = 0;
int amount = 0;
for (AmountInfo amountInfo : amountInfoList) {
value += amountInfo.getValue();
amount += amountInfo.getAmount();
}
AmountInfo result = new AmountInfo(value, amount);
英文:
define a DTO like this:
class AmountInfo {
int value;
int amount;
}
use the follow code to get what you want:
int value = 0;
int amount = 0;
for (AmountInfo amountInfo : amountInfoList) {
value += amountInfo.getValue();
amount += amountInfo.getAmount();
}
AmountInfo result = new AmountInfo(value, amount);
答案2
得分: 0
你应该拥有一个表示输入列表中项目的POJO,并且可以实现add(Pojo)
方法来累积多个POJO的总值:
public class Pojo {
private int amount;
private int value;
// 获取器/设置器/构造函数
public void add(Pojo pojo) {
this.amount += pojo.amount;
this.value += pojo.value;
}
}
测试(读取JSON,计算总值,写入结果):
String json = "[{\n"
+ " \"amount\": 100,\n"
+ " \"value\": 200\n"
+ "},\n"
+ "{\n"
+ " \"amount\": 200,\n"
+ " \"value\": 200\n"
+ "}]";
ObjectMapper mapper = new ObjectMapper();
List<Pojo> list = mapper.readValue(json, new TypeReference<List<Pojo>>() {});
Pojo total = new Pojo();
/*
for (Pojo item : list) {
total.add(item);
}
*/
list.forEach(total::add); // 用 `forEach` 和实例方法引用替代之前的循环
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(total);
System.out.println(result);
输出
{
"amount" : 300,
"value" : 400
}
英文:
You should have a POJO representing the item in the input list and you can implement method add(Pojo)
to accumulate total value for multiple POJOs:
public class Pojo {
private int amount;
private int value;
// getters/setters/constructor(s)
public void add(Pojo pojo) {
this.amount += pojo.amount;
this.value += pojo.value;
}
}
Test (reading JSON, calculating total, writing the result):
String json = "[{\n"
+ " \"amount\": 100,\n"
+ " \"value\": 200\n"
+ "},\n"
+ "{\n"
+ " \"amount\": 200,\n"
+ " \"value\": 200\n"
+ "}]";
ObjectMapper mapper = new ObjectMapper();
List<Pojo> list = mapper.readValue(json, new TypeReference<>() {});
Pojo total = new Pojo();
/*
for (Pojo item : list) {
total.add(item);
}
*/
list.forEach(total::add); // replacing previous loop with `forEach` and instance method reference
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(total);
System.out.println(result);
output
{
"amount" : 300,
"value" : 400
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论