目录
一.Java8
二.行为参数化
2.1 筛选绿苹果
2.2 把颜色作为参数
2.3 筛选轻重苹果
2.4 对每个属性进行筛选
2.5 通过策略模式改变
2.6 抽象行为
2.7 使用匿名类
三.lambda表达式
四.使用lambda表达式
4.1 lambda使用场景
4.2 函数式接口
4.3 @Functionallnerface
public static List < Apple > filterGreenApples ( List < Apple > inventory ) {List < Apple > result = new ArrayList <> ();for ( Apple apple : inventory ){if ( "green" . equals ( apple . getColor () ) {result . add ( apple );}}return result ;}
public static List < Apple > filterApplesByColor ( List < Apple > inventory ,String color ) {List < Apple > result = new ArrayList <> ();for ( Apple apple : inventory ){if ( apple . getColor (). equals ( color ) ) {result . add ( apple );}}return result ;}
public static List < Apple > filterApplesByWeight ( List < Apple > inventory ,int weight ) {List < Apple > result = new ArrayList <> ();For ( Apple apple : inventory ){if ( apple . getWeight () > weight ){result . add ( apple );}}return result ;}
对苹果进行筛选的部分代码大量重复,从工程工作量的角 度来看,这代价太大了。
public static List < Apple > filterApples ( List < Apple > inventory ,String color ,int weight ,boolean flag ) {List < Apple > result = new ArrayList <> ();for ( Apple apple : inventory ){if ( ( flag && apple . getColor (). equals ( color )) ||( ! flag && apple . getWeight () > weight ) ){result . add ( apple );}}return result ;}
ListgreenApples = filterApples(inventory, "green", 0, true); ListheavyApples = filterApples(inventory
public interface ApplePredicate{boolean test (Apple apple);}
有了这个谓词接口,便可以实现多个不同的标准
public class AppleHeavyWeightPredicate implements ApplePredicate {public boolean test ( Apple apple ){return apple . getWeight () > 150 ;}}
public class AppleGreenColorPredicate implements ApplePredicate {public boolean test ( Apple apple ){return "green" . equals ( apple . getColor ());}}
public static List < Apple > filterApples ( List < Apple > inventory ,ApplePredicate p ){List < Apple > result = new ArrayList <> ();for ( Apple apple : inventory ){if ( p . test ( apple )){result . add ( apple );}}return result ;}
public class AppleRedAndHeavyPredicate implements ApplePredicate {public boolean test ( Apple apple ){return "red" . equals ( apple . getColor ())&& apple . getWeight () > 150 ;}}List < Apple > redAndHeavyApples =filterApples ( inventory , new AppleRedAndHeavyPredicate ());
分析:test方法中放的就是filterApples所需要执行的行为
什么是行为参数化?多种行为,一个参数
通过这种方式,我们可以把行为抽象出来,让代码适应需求的变化,但这个过程很繁琐,因为你
List < Apple > redApples = filterApples ( inventory , new ApplePredicate () {public boolean test ( Apple apple ){return "red" . equals ( apple . getColor ());}});
但匿名类还是不够好,它往往很笨重,因为它占用了很多空间。
未使用:
Comparator < Apple > byWeight = new Comparator < Apple > () {public int compare ( Apple a1 , Apple a2 ){return a1 . getWeight (). compareTo ( a2 . getWeight ());}};
使用后:
Comparator < Apple > byWeight =( Apple a1 , Apple a2 ) -> a1 . getWeight (). compareTo ( a2 . getWeight ());
List result = filterApples (inventory ,( Apple apple ) -> "red" . equals ( apple . getColor ()));
我们可以将任何的函数式接口改写为lambda的调用方式。
函数式接口就是只定义一个抽象方法的接口。
public interface Predicate < T > {boolean test ( T t );}
public interface Comparator < T > { //java.util.Comparatorint compare ( T o1 , T o2 );}public interface Runnable { //java.lang.Runnablevoid run ();}public interface ActionListener extends EventListener {//java.awt.event.ActionListenervoid actionPerformed ( ActionEvent e );}public interface Callable < V > { //java.util.concurrent.CallableV call ();}public interface PrivilegedAction < V > {//java.security.PrivilegedActionV run ();}