[英]update an existing object in list
Q: I have a Bank class containing multiple loan accounts (LoanAccount class).
问:我有一个包含多个贷款账户(LoanAccount 类)的银行 class。 I've create a LoanAccountService that have the CRUD functionalities.
我创建了一个具有 CRUD 功能的 LoanAccountService。 My concerns are about how I implemented the update functionality.
我担心的是我如何实现更新功能。
Bank银行
public class Bank {
private List<LoanAccount> loanAccounts;
}
Loan account贷款账户
public class LoanAccount {
private String id;
private Integer numberOfInstallments;
private LoanAccountType type;
private Date creationDate;
private BigDecimal loanAmount;
}
Service服务
public class LoanAccountService{
private Bank bank;
public LoanAccountService(Bank bank) {
this.bank = bank;
}
public LoanAccount update(LoanAccount loanAccount) {
Optional<LoanAccount> account = bank.getLoanAccounts()
.stream()
.filter(la -> la.getId().equals(loanAccount.getId()))
.findAny();
if (account.isPresent()) {
account.get().setCreationDate(loanAccount.getCreationDate());
account.get().setLoanAmount(loanAccount.getLoanAmount());
account.get().setNumberOfInstallments(loanAccount.getNumberOfInstallments());
account.get().setType(loanAccount.getType());
} else {
throw new IllegalArgumentException("The object does not exist.");
}
return loanAccount;
}
}
When the method update is called with a LoanAccount containing an id that already exists in loanAccounts list, I want to update the existing object with the object loanAccount given as parameter.当使用包含 LoanAccounts 列表中已经存在的 id 的 LoanAccount 调用方法更新时,我想使用给定的 object loanAccount 作为参数更新现有的 object。
Above is my implementation, but I feel like there should be better ways to do it.以上是我的实现,但我觉得应该有更好的方法来做到这一点。
Use Builder for getter and setter将 Builder 用于 getter 和 setter
public class LoanAccount { private String id; private Integer numberOfInstallments; // add other properties public String getId() { return id; } public LoanAccount setId(String id) { this.id = id; return this; } public Integer getNumberOfInstallments() { return numberOfInstallments; } public LoanAccount setNumberOfInstallments(Integer numberOfInstallments) { this.numberOfInstallments = numberOfInstallments; return this; }
Use this one for update method使用此更新方法
public LoanAccount update(LoanAccount loanAccount) { return bank.getLoanAccounts().stream().filter(la -> la.getId().equals(loanAccount.getId())).findFirst().orElseThrow(IllegalArgumentException::new).setCreationDate(loanAccount.getCreationDate()).setLoanAmount(loanAccount.getLoanAmount()).setNumberOfInstallments(loanAccount.getNumberOfInstallments()).setType(loanAccount.getType()); }
You could use a HashMap where the TKey is the type of your LoanAccount.id.您可以使用 HashMap,其中 TKey 是 LoanAccount.id 的类型。 Then call
loanAccounts.put(id, object)
This will update the object if there is already an Id and add a new object if not.然后调用
loanAccounts.put(id, object)
如果已经有一个Id,这将更新object,如果没有,则添加一个新的object。
This is a cheap, dirty way.这是一种廉价、肮脏的方式。 Another way of doing it would be to make your LoanAccount class implement
Comparable
and in the compareTo()
method make a id based comparation.另一种方法是让您的 LoanAccount class 实现
Comparable
并在compareTo()
方法中进行基于 id 的比较。 Do the same thing overriding your equals()
and you should be ready to go.做同样的事情覆盖你的
equals()
,你应该准备好go。
@Override
public boolean equals(object obj) {
if (obj == null) return false;
return ((LoanAccount)obj).getId() == this.getId();
}
something like that.类似的东西。 (code wrote by memory, can have errors and lacks validations like the data type)
(由 memory 编写的代码,可能会出现错误并且缺少数据类型等验证)
Why not you use to find and update the records instead of using the above points?您为什么不使用查找和更新记录而不是使用上述几点?
These questions may give you an idea.这些问题可能会给你一个想法。 If you answering it !!!
如果你回答它! If not let we discuss deeper
如果不是让我们更深入地讨论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.