英文:
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<Benefits> clone(List<Allowance> allowanceList){
List<Benefits> benefitList = new ArrayList<>();
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 "[ itemId : " + this.itemId + ", bName : " + this.bName + ", remaining : " + this.remaining + ", bTotal :" + this.bTotal + "]";
}
}
To create the list of Benefit items, you could also create the following List directly:
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));
答案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<Allowance> AList = BList.stream().map(B -> {
new Allowance(
B.getId(),
B.getName(),
B.getRemainingAmount(),
B.bTotal())
}).collect(Collectors.toList())
答案3
得分: 0
根据我阅读的对你原始问题的回复,你似乎在寻找类似以下方式的解决方案:
- 你需要为 Allowance 的所有属性创建 getters 方法。
- 然后创建一个以 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;
-
You need to create getters for all the attributes of Allowance.
-
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; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论