如何将一个列表的值复制到另一个列表中的值,使用Java?

huangapple go评论76阅读模式
英文:

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 = &quot;[{\n&quot;
                + &quot;    \&quot;amount\&quot;: 100,\n&quot;
                + &quot;    \&quot;value\&quot;: 200\n&quot;
                + &quot;},\n&quot;
                + &quot;{\n&quot;
                + &quot;    \&quot;amount\&quot;: 200,\n&quot;
                + &quot;    \&quot;value\&quot;: 200\n&quot;
                + &quot;}]&quot;;

ObjectMapper mapper = new ObjectMapper();

List&lt;Pojo&gt; list = mapper.readValue(json, new TypeReference&lt;&gt;() {});

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

{
  &quot;amount&quot; : 300,
  &quot;value&quot; : 400
}

huangapple
  • 本文由 发表于 2020年10月15日 17:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64368339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定