Please bear with me for silly question, I am new to Java8. I have to write the following code using lambda and apply the condition on the two attribute. I am able to fetch one and apply predicate on it.
for (EventTypePricingMapping eventTypePricingMapping : eventTypePricingMappings) {
BigDecimal feePerRevenue = eventTypePricingMapping.getFeePerRevenue();
if (feePerRevenue != null && feePerRevenue.intValue() < 0) {
throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");
}
if (eventTypePricingMapping.getFeePerReg().intValue() < 0) {
throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");
}
}
In the below code how can i fetch the value of feePerRevenue
and apply predicate on it, as i have done for that getfeePerReg
in the below code.
boolean isRegFeeInvalid = globalPricingRequests.stream()
.map(GlobalPricingRequest::getEventTypePricingList)
.flatMap(List::stream)
.map(EventTypePricingMapping::getFeePerReg)
.anyMatch(criteria);
My predicate is like as below :
Predicate<BigDecimal> criteria = value -> value != null && value.signum() < 0;
I have tried the below code as per suggestion but i am not able to fetch both the values in map() in the
boolean isRegFeeInvalid = globalPricingRequests.stream()
.map(GlobalPricingRequest::getEventTypePricingList)
.flatMap(List::stream)
.map(EventTypePricingMapping::getFeePerReg,EventTypePricingMapping::getFeePerReg)
.anyMatch(verifyFeePerRegAndFeePerRevenue());
private static boolean verifyFeePerRegAndFeePerRevenue(BigDecimal feePerReg, BigDecimal feePerRevenue) {
Predicate<BigDecimal> criteria = value -> value != null && value.signum() < 0;
return criteria.test(feePerReg) && criteria.test(feePerRevenue);
}
How to fetch two values in line map(EventTypePricingMapping::getFeePerReg,EventTypePricingMapping::getFeePerReg)
and pass them in anyMatch operator
map
<R> Stream<R> map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
This is an intermediate operation.
So i will suggest to create two different List
, one for feePerReg
and another for feePerRevenue
feePerReg
List<BigDecimal> feeperReg = globalPricingRequests.stream().flatMap(eventPrice->eventPrice.getEventTypePricingList().stream()).map(eventType->eventType.getFeePerReg()).collect(Collectors.toList());
feePerRevenue
List<BigDecimal> feePerRevenue = globalPricingRequests.stream().flatMap(eventPrice->eventPrice.getEventTypePricingList().stream()).map(eventType->eventType.getFeePerRevenue()).collect(Collectors.toList());
Predicate Predicate to check for both lists
Predicate<BigDecimal> criteria = value -> value != null && value.signum() < 0;
verifyFeePerRegAndFeePerRevenue method that takes two List<BigDecimal>
as arguments
private static boolean verifyFeePerRegAndFeePerRevenue(List<BigDecimal> feePerReg, List<BigDecimal> feePerRevenue) {
Predicate<BigDecimal> criteria = value -> value != null && value.signum() < 0;
return feePerReg.stream().anyMatch(criteria) && feePerRevenue.stream().anyMatch(criteria);
}
Case 2
Instead of two different List
, you can have only one list with eventTypePricingMapping
and Predicate
condition on both feePerReg
and feePerRevenue
eventTypePricingMapping
List<EventTypePricingMapping> result = globalPricingRequests.stream().flatMap(eventPrice->eventPrice.getEventTypePricingList().stream()).collect(Collectors.toList());
Predicate Predicate to check for both
Predicate<EventTypePricingMapping> criteria = value -> value.getFeePerRevenue() != null && value.getFeePerReg()!= null && value.getFeePerRevenue() < 0 && value.getFeePerReg < 0;
verifyFeePerRegAndFeePerRevenue method takes List<EventTypePricingMapping>
apply Predicate
on list
private static boolean verifyFeePerRegAndFeePerRevenue(List<EventTypePricingMapping> price) {
Predicate<EventTypePricingMapping> criteria = value -> value.getFeePerRevenue() != null && value.getFeePerReg()!= null && value.getFeePerRevenue() < 0 && value.getFeePerReg < 0;
return price.stream().anyMatch(criteria);
}