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 ;}
List<Apple> greenApples = filterApples(inventory, "green", 0, true);List<Apple> heavyApples = 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 ();}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务