bairava surya wrote:As am new to java 8 can you provide the real time example of lambda purpose and its inclusion in java 8(play ground)
Suppose this simple problem: print all the odd numbers in a list of integers greater than 15 without any duplicates.
In Java 7 you'll probable write something like:
The same code in Java 8:
So you'll see immediately 2 (main) advantages:
the Java 8 code is more concise, but it's still easy to read (maybe even better) and the compile-time safety (we all love so much) is still present as well (so the compiler knows i is an Integer)
with java 8 you have a well-designed library, so you can concentrate just on what needs to be done. The predicate i -> i > 15 && i % 2 == 1 is the only thing I had to write myself, all the other code is nothing more than using the library
And as a final note: let's assume the business requirement changes and duplicates are allowed. In the Java 7 code you have to change Set with List and HashSet with ArrayList; but in the Java 8 code you'll just remove the .distinct() line.
It's not on the OCA 7/8 exam, so we won't elaborate on this new feature in Java 8 in this forum (to avoid confusion for other aspirants preparing for the OCA 7/8 exam). Just start a new thread with your doubts/questions in the SCJP/OCPJP forum.