There are two ways to implement a functional interface, that refers to this:
- Lambda expression:
fot -> reduceByThis(fot)
- Method reference:
this::reduceByThis
A lambda expression can access this
in the same way as the enclosing scope. That means you can invoke the method reduceByThis
both with and without qualification. For the method reference, that target object can be explicitly bound. The following example only shows the latter:
public List<FriendOfTarget> reduce() {
return completeListOfFriendOfTargets
.stream()
.filter(this::reduceByThis)
.collect(toList());
}
You can also use this
explicitly in lambda expressions. As I have already mentioned, it refers to the this
of the enclosing scope (in contrast to anonymous inner classes). For example:
public List<FriendOfTarget> reduce() {
return completeListOfFriendOfTargets
.stream()
.filter(fot -> fot.reduceOtherDirection(this))
.collect(toList());
}