如何使用Java中的函数列表来比较两个对象';字段和求和结果

如何使用Java中的函数列表来比较两个对象';字段和求和结果,java,function,java-stream,Java,Function,Java Stream,我有两个对象可以使用不同的函数对函数列表中的每个字段逐个字段进行比较 目标:使用函数列表调用compare(NeedCompare n1,NeedCompare n2)并获得 BigDecimal值=比较计数(需要比较需要比较1,需要比较需要比较2) .add(比较值(需要比较需要比较1,需要比较需要比较2) .add(compareName(需要比较需要比较1,需要比较需要比较2) .add(比较类别(需要比较需要比较1,需要比较需要比较2‘‘‘‘)’) 用溪流做就完美了 public cla

我有两个对象可以使用不同的函数对函数列表中的每个字段逐个字段进行比较

目标:使用函数列表调用
compare(NeedCompare n1,NeedCompare n2)
并获得

BigDecimal值=比较计数(需要比较需要比较1,需要比较需要比较2)
.add(比较值(需要比较需要比较1,需要比较需要比较2)
.add(compareName(需要比较需要比较1,需要比较需要比较2)
.add(比较类别(需要比较需要比较1,需要比较需要比较2‘‘‘‘)’)

用溪流做就完美了

public class NeedCompare {
private BigDecimal category;
private String name;
private BigDecimal value;
private BigDecimal amount;

// The list is wrong and I don't know how to organize it
private List<Function<BigDecimal, NeedCompare>> functions = Arrays.asList(
        (needCompare1, needCompare2) -> compareCategory(needCompare1, needCompare2),
        (needCompare1, needCompare2) -> compareName(needCompare1, needCompare2),
        (needCompare1, needCompare2) -> compareValue(needCompare1, needCompare2),
        (needCompare1, needCompare2) -> compareAmount(needCompare1, needCompare2)

);

public BigDecimal compare(NeedCompare n1, NeedCompare n2) {
    //The method is broken. I need to return a sum of the results of the functions
    return functions.stream().map(function -> function(n1, n2)).reduce(BigDecimal.ZERO, BigDecimal::add);
}

private BigDecimal compareAmount(NeedCompare needCompare1, NeedCompare needCompare2) {
    // Complex function to compare category of NeedCompare
    return needCompare1.getCategory().equals(needCompare2.getCategory()) ? BigDecimal.ZERO : BigDecimal.ONE;
}

private BigDecimal compareValue(NeedCompare needCompare1, NeedCompare needCompare2) {
    // Complex function to compare name of NeedCompare
    return needCompare1.getName().equals(needCompare2.getName()) ? BigDecimal.ZERO : BigDecimal.ONE;
}

private BigDecimal compareName(NeedCompare needCompare1, NeedCompare needCompare2) {
    // Complex function to compare value of NeedCompare
    return needCompare1.getValue().equals(needCompare2.getValue()) ? BigDecimal.ZERO : BigDecimal.ONE;
}

private BigDecimal compareCategory(NeedCompare needCompare1, NeedCompare needCompare2) {
    // Complex function to compare amount of NeedCompare
    return needCompare1.getAmount().equals(needCompare2.getAmount()) ? BigDecimal.ZERO : BigDecimal.ONE;
}
// Getters and setters ...
公共类需要比较{
私有大十进制类;
私有字符串名称;
私有大十进制值;
私人金额;
//名单错了,我不知道怎么组织
私有列表函数=Arrays.asList(
(needCompare1,needCompare2)->比较类别(needCompare1,needCompare2),
(needCompare1,needCompare2)->compareName(needCompare1,needCompare2),
(needCompare1,needCompare2)->compareValue(needCompare1,needCompare2),
(needCompare1,needCompare2)->compareAmount(needCompare1,needCompare2)
);
公共BigDecimal比较(NeedCompare n1,NeedCompare n2){
//方法已损坏。我需要返回函数结果的总和
返回functions.stream().map(函数->函数(n1,n2)).reduce(BigDecimal.ZERO,BigDecimal::add);
}
私有BigDecimal比较计数(需要比较需要比较1,需要比较需要比较2){
//比较NeedCompare类别的复杂函数
返回needCompare1.getCategory().equals(needCompare2.getCategory())?BigDecimal.ZERO:BigDecimal.ONE;
}
私有BigDecimal比较值(需要比较需要比较1,需要比较需要比较2){
//用于比较NeedCompare名称的复杂函数
返回needCompare1.getName().equals(needCompare2.getName())?BigDecimal.ZERO:BigDecimal.ONE;
}
私有BigDecimal compareName(需要比较需要比较1,需要比较需要比较2){
//比较NeedCompare值的复杂函数
返回needCompare1.getValue().equals(needCompare2.getValue())?BigDecimal.ZERO:BigDecimal.ONE;
}
私有BigDecimal比较类别(需要比较需要比较1,需要比较需要比较2){
//用于比较需要量比较的复杂函数
返回needCompare1.getAmount().equals(needCompare2.getAmount())?BigDecimal.ZERO:BigDecimal.ONE;
}
//接球手和接球手。。。

您拥有的函数有两个参数,因此它们是
BiFunction
(首先是参数类型,最后是返回/目标类型)。因此您拥有
私有列表函数

此外,通过使用方法引用而不是lambdas,您可以简单地使列表更具可读性:

private List<BiFunction<NeedCompare, NeedCompare, BigDecimal>> functions 
    = Arrays.asList(this::compareCategory, this::compareName, 
        this::compareValue, this::compareAmount);
私有列表函数
=Arrays.asList(this::compareCegory,this::compareName,
this::compareValue,this::compareAmount);

Sweet,非常感谢。现在我可以使用流进行求和,比如
public BigDecimal compare(NeedCompare n1,NeedCompare n2){返回函数.stream().map(f->f.apply(n1,n2)).reduce(BigDecimal.ZERO,BigDecimal::add);