lamda表达式在Java8中的使用
我想重构这段代码以使用lambda表达式java8lamda表达式在Java8中的使用,java,lambda,java-stream,Java,Lambda,Java Stream,我想重构这段代码以使用lambda表达式java8 for(int i=0;i customer.getINCOME().getGROSSMONTH1().Tobiginger()< customer.getINCOME().getNETTSALMONTH1().toBigInteger()) .findAny().ifPresent(c->{ 日志错误(“”) 抛出新的RuntimeException(); });; 您正在迭代customers.getCUSTOMER()的元素,该元素似乎是
for(int i=0;i
customers.getCUSTOMER().stream().forEach(客户->{
if(customer.getINCOME().getGROSSMONTH1().toBigInteger()
您还应该尝试使用camelcase重命名您的方法,例如getIncome(),以使其更易于阅读并符合正常的Java编写标准 customers.getCUSTOMER().forEach(客户->{
customers.getCUSTOMER().forEach(customer -> {
if (customer.getINCOME().getGROSSMONTH1().toBigInteger() <
customer.getINCOME().getNETTSALMONTH1().toBigInteger()) {
log.error("")
throw new RuntimeException();
}
});
if(customer.getINCOME().getGROSSMONTH1().tobiginger()<
customer.getINCOME().getNETTSALMONTH1().toBigInteger()){
日志错误(“”)
抛出新的RuntimeException();
}
});
简单的方法
使用stream和Optional的另一种方法:
customers.stream().filter(customer ->
customer.getINCOME().getGROSSMONTH1().toBigInteger() <
customer.getINCOME().getNETTSALMONTH1().toBigInteger())
.findAny().ifPresent(c -> {
log.error("")
throw new RuntimeException();
});;
customers.stream().filter(客户->
customer.getINCOME().getGROSSMONTH1().Tobiginger()<
customer.getINCOME().getNETTSALMONTH1().toBigInteger())
.findAny().ifPresent(c->{
日志错误(“”)
抛出新的RuntimeException();
});;
您正在迭代customers.getCUSTOMER()
的元素,该元素似乎是一个列表(为了回答这个问题,我假设它是一个)。那么,您大概要处理该列表的流:
customers.getCUSTOMER().stream()
每个元素的getINCOME()
都使用了两次,而不是它的任何其他方面,因此您可能希望通过该方法映射元素。假设客户列表的元素类型为customer
,则可能是
.map(Customer::getINCOME)
有多种方法可以实现这一点,但由于您在满足条件时抛出异常,我个人会选择Stream.anyMatch()
:
由于您正在抛出一个运行时异常
,因此也可以在流中使用lambda来实现这一点,但如果您抛出一个选中的异常,则通常不可能实现这一点。在选中的异常情况下,通常需要类似于此处所示的内容
另一方面,如果您想记录有关失败元素的信息,那么您需要在流中这样做。此外,在这种情况下,您可能希望跳过map()
。相反,您可以基于谓词filter()
,并在元素中保留findFirst()
(如果您不关心它是否是第一个报告的元素,则可以使用findAny()
)。结果是一个可选的,如果存在,您可以使用另一个lambda进行处理:
customers.getCUSTOMER().stream()
.filter(x -> x.getINCOME().getGROSSMONTH1().toBigInteger().compareTo(
x.getINCOME().getNETTSALMONTH1().toBigInteger()) < 0)
.findFirst()
.ifPresent(x -> {
log.error(x.getName() + " invalid: net salary is larger than gross")
throw new RuntimeException();
});
customers.getCUSTOMER().stream()
.filter(x->x.getINCOME().getGROSSMONTH1().Tobiginger().compareTo(
x、 getINCOME().getNETTSALMONTH1().Tobiginter())<0)
.findFirst()
.如果存在(x->{
log.error(x.getName()+“无效:净工资大于毛工资”)
抛出新的RuntimeException();
});
请注意,这并不能解决选中的异常问题。不能从ifPresent
lambda内部抛出选中的异常。通过使用java-8流,对条件使用filter
,并在一次匹配后终止
customers.getCUSTOMER().stream()
.filter(c->c.getINCOME().getGROSSMONTH1().toBigInteger() < c.getINCOME().getNETTSALMONTH1().toBigInteger())
.findFirst()
.ifPresent(cu->{
log.error("")
throw new RuntimeException();
});
customers.getCUSTOMER().stream()
.filter(c->c.getINCOME().getGROSSMONTH1().Tobiginter(){
日志错误(“”)
抛出新的RuntimeException();
});
在这种情况下,您需要的是一个BiPredicate
,如下所示
BiPredicate<TypeOfMonths> biPredicate = (tom1, tom2) -> tom1.toBigInteger() < tom2.toBigInteger();
boolean shouldThrowException = customers.getCUSTOMER()
.stream()
.anyMatch(cust -> {
TypeOfMonth tom1 = getINCOME().getGROSSMONTH1();
TypeOfMonth tom2 = getINCOME()..getNETTSALMONTH1();
return biPredicate.test(tom1, tom2);
});
if (shouldThrowException) {
log.error("");
throw new RuntimeException();
}
@Paul可能重复-这是一个未经检查的异常,因此抛出它没有问题。顺便说一句,你不能使用比较biginger
,好的,所以customers.getCUSTOMER().stream().filter(c->c.getINCOME().getGROSSMONTH1().tobigineger().compareTo(c.getINCOME().getnetsalmonth1().tobiginter1())=-1.findAny().ifPresent(c->{throw new RuntimeException();})代码>应该足够了。@StephenC我先将它们转换为整数,然后使用
customers.getCUSTOMER().stream()
.filter(c->c.getINCOME().getGROSSMONTH1().toBigInteger() < c.getINCOME().getNETTSALMONTH1().toBigInteger())
.findFirst()
.ifPresent(cu->{
log.error("")
throw new RuntimeException();
});
BiPredicate<TypeOfMonths> biPredicate = (tom1, tom2) -> tom1.toBigInteger() < tom2.toBigInteger();
boolean shouldThrowException = customers.getCUSTOMER()
.stream()
.anyMatch(cust -> {
TypeOfMonth tom1 = getINCOME().getGROSSMONTH1();
TypeOfMonth tom2 = getINCOME()..getNETTSALMONTH1();
return biPredicate.test(tom1, tom2);
});
if (shouldThrowException) {
log.error("");
throw new RuntimeException();
}