我希望将两个相同域对象的ArrayList组合并求和(特定属性)。根据多个属性匹配的条件,将它们合并为一个。
这是我的模型类
public class SalesTransactionTax {
private String code;
private BigDecimal rate;
private Boolean isFixed;
private BigDecimal taxAmount;
public SalesTransactionTax(String code, BigDecimal rate, Boolean isFixed, BigDecimal taxAmount) { // NO_UCD
// (unused
// code)
this.code = code;
this.rate = rate;
this.isFixed = isFixed;
this.taxAmount = taxAmount;
}
public SalesTransactionTax() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public BigDecimal getRate() {
return rate;
}
public void setRate(BigDecimal rate) {
this.rate = rate;
}
public Boolean getIsFixed() {
return isFixed;
}
public void setFixed(Boolean fixed) {
isFixed = fixed;
}
public BigDecimal getTaxAmount() {
return taxAmount;
}
public void setTaxAmount(BigDecimal taxAmount) {
this.taxAmount = taxAmount;
}
@Override
public String toString() {
return "SalesTransactionTax [code=" + code + ", rate=" + rate + ", isFixed=" + isFixed
+ ", taxAmount=" + taxAmount + "]";
}
}
以下面的例子为例:
List<SalesTransactionTax> taxes = new ArrayList<>();
taxes.add(new SalesTransactionTax("VAT_1", 5.000, true, 1.100));
taxes.add(new SalesTransactionTax("VAT_1", 5.000, true, 1.100));
taxes.add(new SalesTransactionTax("VAT_2", 9.000, true, 2.200));
taxes.add(new SalesTransactionTax("VAT_2", 9.000, true, 2.200));
taxes.add(new SalesTransactionTax("VAT_2", 9.000, true, 2.200));
taxes.add(new SalesTransactionTax("VAT_2", 9.000, false, 9.500));
taxes.add(new SalesTransactionTax("VAT_3", 7.000, true, 1.000));
新ArrayList中的值为:
使用Java 8,可能会有一个Lambda表达式吗?感谢您的任何建议。
答案 0 :(得分:4)
add (...)
方法应该具有对税额进行求和的逻辑,为此,有必要检查新交易是否符合既定的验证。
验证应该相同:getCode(), getRate(), getIsFixed()
要添加的代码段
public void add(SalesTransactionTax inputValue) {
SalesTransactionTax salesTransactionTax = checkInList(inputValue);
if (salesTransactionTax != null) {
// To sum operations the method used is add() from BigDecimal
salesTransactionTax.setTaxAmount(salesTransactionTax.getTaxAmount().add(inputValue.getTaxAmount()));
} else {
this.salesTransactionTaxList.add(inputValue);
}
}
要检查列表中是否存在新的SalesTransactionTax
,您可以使用Java 8中的filter()
方法进行验证
检查验证的代码段:
private SalesTransactionTax checkInList(SalesTransactionTax stt) {
return salesTransactionTaxList.stream()
// To compare String is used equals()
.filter(stt2 -> stt2.getCode().equals(stt.getCode()))
// To compare BigDecimal is used compareTo()
.filter(stt2 -> stt2.getRate().compareTo(stt.getRate()) == 0)
// To compare boolean is used ==
.filter(stt2 -> stt2.getIsFixed() == stt.getIsFixed())
// If doesn't exist return null
.findAny().orElse(null);
}
完整示例:
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class SalesTransactionTax {
private String code;
private BigDecimal rate;
private Boolean isFixed;
private BigDecimal taxAmount;
private List<SalesTransactionTax> salesTransactionTaxList = null;
public SalesTransactionTax(String code, BigDecimal rate, Boolean isFixed, BigDecimal taxAmount) { // NO_UCD
this.code = code;
this.rate = rate;
this.isFixed = isFixed;
this.taxAmount = taxAmount;
}
public SalesTransactionTax() {
this.salesTransactionTaxList = new ArrayList<>();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public BigDecimal getRate() {
return rate;
}
public void setRate(BigDecimal rate) {
this.rate = rate;
}
public Boolean getIsFixed() {
return isFixed;
}
public void setFixed(Boolean fixed) {
isFixed = fixed;
}
public BigDecimal getTaxAmount() {
return taxAmount;
}
public void setTaxAmount(BigDecimal taxAmount) {
this.taxAmount = taxAmount;
}
public void add(SalesTransactionTax inputValue) {
SalesTransactionTax salesTransactionTax = checkInList(inputValue);
if (salesTransactionTax != null) {
// To sum operations the method used is add() from BigDecimal
salesTransactionTax.setTaxAmount(salesTransactionTax.getTaxAmount().add(inputValue.getTaxAmount()));
} else {
this.salesTransactionTaxList.add(inputValue);
}
}
private SalesTransactionTax checkInList(SalesTransactionTax stt) {
return salesTransactionTaxList.stream()
// To compare String is used equals()
.filter(stt2 -> stt2.getCode().equals(stt.getCode()))
// To compare BigDecimal is used compareTo()
.filter(stt2 -> stt2.getRate().compareTo(stt.getRate()) == 0)
// To compare boolean is used ==
.filter(stt2 -> stt2.getIsFixed() == stt.getIsFixed())
// If doesn't exist return null
.findAny().orElse(null);
}
public void print() {
salesTransactionTaxList.forEach(System.out::println);
}
@Override
public String toString() {
//Output formatted
DecimalFormat df = new DecimalFormat("#,###.000");
return code + ", " + df.format(new BigDecimal(String.valueOf(rate))) + ", " + isFixed + ", " + df.format(new BigDecimal(String.valueOf(taxAmount)));
}
public static void main(String[] args) {
SalesTransactionTax mergeTaxes = new SalesTransactionTax();
mergeTaxes.add(new SalesTransactionTax("VAT_1", BigDecimal.valueOf(5.000), true, BigDecimal.valueOf(1.100)));
mergeTaxes.add(new SalesTransactionTax("VAT_1", BigDecimal.valueOf(5.000), true, BigDecimal.valueOf(1.100)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), false, BigDecimal.valueOf(9.500)));
mergeTaxes.add(new SalesTransactionTax("VAT_3", BigDecimal.valueOf(7.000), true, BigDecimal.valueOf(1.000)));
mergeTaxes.print();
}
}
<强>输出:强>
VAT_1, 5.000, true, 2.200
VAT_2, 9.000, true, 6.600
VAT_2, 9.000, false, 9.500
VAT_3, 7.000, true, 1.000
答案 1 :(得分:0)
你说
根据条件,如果两个属性匹配,则将它们组合在一起 之一。
但根据示例结果,您需要检查三个属性匹配。
public class SalesTransactionMerge {
private List<SalesTransactionTax> taxes = null;
public List<SalesTransactionTax> getTaxes(){
return this.taxes;
}
public SalesTransactionMerge(){
this.taxes = new ArrayList<SalesTransactionTax>();
}
public void add(SalesTransactionTax stt){
SalesTransactionTax sttExists = exists(stt);
if(sttExists == null){
this.taxes.add(stt);
}
else{
sttExists.setTaxAmount(sttExists.getTaxAmount().add(stt.getTaxAmount()));
}
}
private SalesTransactionTax exists(SalesTransactionTax stt){
SalesTransactionTax sttExists = null;
for(SalesTransactionTax stt2 : taxes){
int matches = 0;
if(stt2.getCode().equals(stt.getCode())) matches++;
if(stt2.getRate().compareTo(stt.getRate()) == 0) matches++;
if(stt2.getIsFixed() == stt.getIsFixed()) matches++;
if(matches == 3){
sttExists = stt2;
break;
}
}
return sttExists;
}
public void print(){
for(SalesTransactionTax stt : taxes){
System.out.println(stt.toString());
}
}
}
使用您的示例
public static void main(String[] args) {
SalesTransactionMerge mergeTaxes = new SalesTransactionMerge();
mergeTaxes.add(new SalesTransactionTax("VAT_1", BigDecimal.valueOf(5.000), true, BigDecimal.valueOf(1.100)));
mergeTaxes.add(new SalesTransactionTax("VAT_1", BigDecimal.valueOf(5.000), true, BigDecimal.valueOf(1.100)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), true, BigDecimal.valueOf(2.200)));
mergeTaxes.add(new SalesTransactionTax("VAT_2", BigDecimal.valueOf(9.000), false, BigDecimal.valueOf(9.500)));
mergeTaxes.add(new SalesTransactionTax("VAT_3", BigDecimal.valueOf(7.000), true, BigDecimal.valueOf(1.000)));
mergeTaxes.print();
}