Java Reference
In-Depth Information
/** An Acceptor accepts some elements from a Collection */
public
public interface
interface CameraAcceptor
CameraAcceptor {
boolean
boolean choose ( Camera c );
}
Now the search application provides a method:
public
public List < Camera > search ( CameraAcceptor acc );
which we can call with code like this (assuming you're comfortable with anonymous inner
classes):
results = searchApp . search ( new
new CameraAcceptor () {
public
public boolean
boolean choose ( Camera c ) {
return
return c . isIlc () && c . getPrice () < 500 ;
}
}
Or, if you were not comfortable with anonymous inner classes, you might have to type
class
class MyIlcPriceAcceptor
MyIlcPriceAcceptor implements
implements CameraAcceptor {
public
public boolean
boolean choose ( Camera c ) {
return
return c . isIlc () && c . getPrice () < 500 ;
}
}
CameraAcceptor myIlcPriceAcceptor = nwq MyIlcPriceAcceptor ();
results = searchApp . search ( myIlcPriceAcceptor );
That's really a great deal of typing just to get one method packaged up for sending into the
search engine. Java's support for lambda expressions or Closures was argued about for many
years (literally) before the experts agreed on how to do it. And the result is staggeringly
simple. One way to think of Java lambda expressions is that each one is just a method that
implements a functional interface . With lambda expressions, you can rewrite the preceding
as just:
results = searchApp . search ( c -> c . isIlc () && c . getPrice () < 500 );
Search WWH ::




Custom Search