May 11, 2013

Java lamda expression in version 8 next year.

Java will have Lamda expression on the coming new version 8 next year. This info is from the official java web site: here

This anonymous class feature was from the day one.
printMembers(
    roster,
    new CheckMember() {
        public boolean test(Member p) {
            return p.getGender() == Member.Sex.MALE
                && p.getAge() >= 18
                && p.getAge() <= 25;
        }
    }
);

But with the new Lamda expression it will become like this:

printMembers(
    roster,
    (Member p) -> p.getGender() == Member.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25
);

Here is how the magik works.
The interface "CheckMember" should have only one member method. The interface that has only one member method is called "functional interface".

interface CheckMember {
    boolean test(Member p);
}

Because functional interface has only one member method, we can omit the obvious function name.

No comments: