将一个列表的变量值设置为另一个列表中的变量值,使用Java。

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

Set variables values of a list to another list variables values in Java

问题

以下是您要的代码部分的翻译:

这是我的津贴类

```java
public class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;
}

这是创建的列表

public List<Allowance> getBList(){
    List<Allowance> BList = new ArrayList<>();
    BList.add(new Allowance("5964852","Name1","DATA","10","458965"));
    BList.add(new Allowance("651655","Name2","DATA","20","5481662"));
    BList.add(new Allowance("123","Name3","VOICE","30","8889625"));
    BList.add(new Allowance("123423","Name4","DATA","20","325489"));
    return BList;
}

这是我的福利类

public class Benefits {
    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;
}

所以,我想要将Allowance类的变量值设置为Benefits类的变量值,就像这样

Id值 --> itemId值

Name值 --> bName值

RemainingAmount值 ---> remaining值

TotalAmount值 ---> bTotal值

然后我想以这种方式返回Benefits列表

["itemId" : "5964852","bName": "Name1","remaining":"10","bTotal":"458965"]

有人可以帮助我吗?

英文:

Here is my Allowance class

public class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;
}

This is created list

public List<Allowance> getBList(){
    List<Allowance> BList = new ArrayList<>();
    List.add(new Allowance("5964852","Name1","DATA","10","458965"));
    List.add(new Allowance("651655","Name2","DATA","20","5481662"));
    List.add(new Allowance("123","Name3","VOICE","30","8889625"));
    List.add(new Allowance("123423","Name4","DATA","20","325489"));
    return BList;
}

Here is my Benefits class

public class Benefits {
    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;
}

So, I want to set Allowance class variables values to Benefits class variables values Like this

Id value --> itemId value

Name value --> bName value

RemainingAmount value ---> remaining value

TotalAmount value ---> bTotal value

Then i want to return Benefits list like this

["itemId" : "5964852","bName": "Name1","remaining":"10","bTotal":"458965"]

Any one can help me ?

答案1

得分: 2

你可以使用 Allowance 实例的单参数构造函数来传输值,然后从传入的 Allowance 实例中填充 Benefits 类的属性。
你还可以重写 Benefits 类的 toString 方法以获得所需的输出。在 Allowance 类中的 clone 方法将接受您已创建的列表,并返回一个 Benefits 实例的列表。

Allowance 类:

class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;

    public String getId() {
        return Id;
    }

    public String getName() {
        return Name;
    }

    public String getType() {
        return Type;
    }

    public String getRemainingAmount() {
        return RemainingAmount;
    }

    public String getTotalAmount() {
        return TotalAmount;
    }

    public Allowance(String id, String name, String type, String remainingAmount, String totalAmount) {
        Id = id;
        Name = name;
        Type = type;
        RemainingAmount = remainingAmount;
        TotalAmount = totalAmount;
    }

    public List<Benefits> clone(List<Allowance> allowanceList){
        
        List<Benefits> benefitList = new ArrayList<>();
        for(Allowance allowance : allowanceList){
            benefitList.add(new Benefits(allowance));
        }
        
        return benefitList;
    }
}

Benefits 类:

class Benefits{

    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;

    public Benefits(Allowance allowance){

        this.itemId = allowance.getId();
        this.bName = allowance.getName();
        this.remaining = allowance.getRemainingAmount();
        this.bTotal = allowance.getTotalAmount();

    }

    @Override
    public String toString() {
        return "[ itemId : " + this.itemId + ", bName : " + this.bName + ", remaining : " + this.remaining + ", bTotal :" + this.bTotal + "]";
    }
}

要创建 Benefits 项的列表,您还可以直接创建以下 List:

List<Benefits> bList = new ArrayList<>();

Allowance a = new Allowance("5964852","Name1","DATA","10","458965");
Allowance b = new Allowance("651655","Name2","DATA","20","5481662");
Allowance c = new Allowance("123","Name3","VOICE","30","8889625");
Allowance d = new Allowance("123423","Name4","DATA","20","325489");

bList.add(new Benefits(a));
bList.add(new Benefits(b));
bList.add(new Benefits(c));
bList.add(new Benefits(d));
英文:

You can transfer the values from an Allowance instance by using the single arg constructor and then poplating the properties of the Benefits class from the Allowance instance that is passed in.
You can also override the toString method of the Benefits class to obtain the desired output. The clone method in the allowance class will take in the list you have already created to return a list of Benefits instances.

The Allowance class:

class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;

    public String getId() {
        return Id;
    }

    public String getName() {
        return Name;
    }

    public String getType() {
        return Type;
    }

    public String getRemainingAmount() {
        return RemainingAmount;
    }

    public String getTotalAmount() {
        return TotalAmount;
    }

    public Allowance(String id, String name, String type, String remainingAmount, String totalAmount) {
        Id = id;
        Name = name;
        Type = type;
        RemainingAmount = remainingAmount;
        TotalAmount = totalAmount;
    }

public List&lt;Benefits&gt; clone(List&lt;Allowance&gt; allowanceList){
        
        List&lt;Benefits&gt; benefitList = new ArrayList&lt;&gt;();
        for(Allowance allowance : allowanceList){
            benefitList.add(new Benefits(allowance));
        }
        
        return benefitList;
    }
}

The benefits class:

class Benefits{

    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;

    public Benefits(Allowance allowance){

        this.itemId = allowance.getId();
        this.bName = allowance.getName();
        this.remaining = allowance.getRemainingAmount();
        this.bTotal = allowance.getTotalAmount();

    }

    @Override
    public String toString() {
        return &quot;[ itemId : &quot; + this.itemId + &quot;, bName : &quot; + this.bName + &quot;, remaining : &quot; + this.remaining + &quot;, bTotal :&quot; + this.bTotal + &quot;]&quot;;
    }
}

To create the list of Benefit items, you could also create the following List directly:

      List&lt;Benefits&gt; bList = new ArrayList&lt;&gt;();

        Allowance a = new Allowance(&quot;5964852&quot;,&quot;Name1&quot;,&quot;DATA&quot;,&quot;10&quot;,&quot;458965&quot;);
        Allowance b = new Allowance(&quot;651655&quot;,&quot;Name2&quot;,&quot;DATA&quot;,&quot;20&quot;,&quot;5481662&quot;);
        Allowance c =  new Allowance(&quot;123&quot;,&quot;Name3&quot;,&quot;VOICE&quot;,&quot;30&quot;,&quot;8889625&quot;);
        Allowance d = new Allowance(&quot;123423&quot;,&quot;Name4&quot;,&quot;DATA&quot;,&quot;20&quot;,&quot;325489&quot;);

        bList.add(new Benefits(a));
        bList.add(new Benefits(b));
        bList.add(new Benefits(c));
        bList.add(new Benefits(d));

答案2

得分: 2

使用流和lambda很容易实现它。

List<Allowance> AList = BList.stream().map(B -> {
    new Allowance(
        B.getId(),
        B.getName(),
        B.getRemainingAmount(),
        B.bTotal())
}).collect(Collectors.toList())
英文:

Use stream and lambda is easy to realize it.

List&lt;Allowance&gt; AList = BList.stream().map(B -&gt; {
    new Allowance(
        B.getId(),
        B.getName(),
        B.getRemainingAmount(),
        B.bTotal())
}).collect(Collectors.toList())

答案3

得分: 0

根据我阅读的对你原始问题的回复,你似乎在寻找类似以下方式的解决方案:

  1. 你需要为 Allowance 的所有属性创建 getters 方法。
  2. 然后创建一个以 Allowance 对象为参数的构造函数。
public Benefits(Allowance a) {
    this.itemId = a.getID;
    this.bName = a.getName;
    this.remaning = a.getRemainingAmount;
    this.bTotal = a.getTotalAmount;
}
英文:

So from what I read in the replies to your original question, you'd be looking for something along these lines;

  1. You need to create getters for all the attributes of Allowance.

  2. Then create a constructor that takes an Allowance object as a parameter.

     public Benefits (Allowance a) {
    
         this.itemId = a.getID;
         this.bName = a.getName;
         this.remaning = a.getRemainingAmount;
         this.bTotal = a.getTotalAmount;
     }
    

huangapple
  • 本文由 发表于 2020年8月16日 14:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63433864.html
匿名

发表评论

匿名网友

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

确定