I'm currently tackling with the issue of transforming for loops which use bitwise operations using Lambdas in Java 8.
Given a set of complex entries, it's required for the loop to traverse ALL entries and call a given method on them (method returns boolean value). Afterwards, return the result.
In other words, I need to call the method on all entries and store the result. The reason behind this is that each entry independently performs a complex operation and must be performed. The end result is a combination of results.
Code snippet:
boolean areAllSuccessful = true;
for (SomeEntry entry : entries) {
areAllSuccessful = areAllSuccessful & entry.doComplexAction(); // keep calling the method on other entries regardless of the result.
}
return areAllSuccessful;
The problem is that the Lambda Functions in Java 8 usually perform short-circuit operations (once the false entry has been detected, the "loop" breaks and the false result is returned).
My best solution so far was to use map/filter/count combination:
return entries
.stream()
.map(entry -> entry.doComplexAction())
.filter(result -> result == false)
.count() > 0
Is there a smarter/cleaner way of doing this?
Thanks!
Q 6 - Which of the following is correct about Java 8 lambda expression? A - Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once).
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Binary AND Operator copies a bit to the result if it exists in both operands. Binary OR Operator copies a bit if it exists in either operand.
Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.
Shouldn't that look something like this:
boolean areAllSuccessful = entries.stream()
.map(entry -> entry.isSuccessful())
.reduce(Boolean.TRUE, Boolean::logicalAnd);
The neatest, and most efficient, way is to use a method reference with allMatch()
return entries.stream().allMatch(SomeEntry::isSuccessful);
If you have 1000's of elements, consider using parallelStream()
instead.
This doesn't process every element (it returns at the first false
), so if your isSuccessful()
method has side effects, it's a bad name and you should rename it or refactor the code to perform the side effects in a process()
(or similar) method and have isSuccessful()
return the result, throwing an IllegalStateException
if process()
hasn't first been called.
If you don't refactor, some developer (including you) will call isSuccessful()
without realising it "does stuff", which could be bad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With