JDK1.8函数式编程Predicate及Consumer的用法

来源:互联网 发布:mac系统恢复出厂设置 编辑:程序博客网 时间:2024/06/17 00:19

JDK1.8函数式编程Predicate及Consumer的用法

Function

Function接口的主要方法

R apply(T t) – 将Function对象应用到输入的参数上,然后返回计算结果。
default <V> Function<T,V> – 将两个Function整合,并返回一个能够执行两个Function对象功能的Function对象。
default <V> Function<T,V> andThen(Function<? super R,? extends V> after) 返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。
default <V> Function<T,V> compose(Function<? super V,? extends T> before)返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象。
static <T> Function<T,T> identity() 返回一个执行了apply()方法之后只会返回输入参数的函数对象。

Function 例子

/** * 定义方法,参数中含有Function * @param valueToBeOperate * @param function */public  void modifyTheValue(int valueToBeOperate,Function<Integer,Integer> function){int newValue = function.apply(valueToBeOperate);//根据输入的参数,然后计算出结果System.out.println(newValue);}
@Testpublic void testFunction(){int incr = 20;int myNumber = 10;modifyTheValue(myNumber,var -> var + incr + 100);//var 相当于myNumber}

结果输出:130 即 10+20+100

FunctionalInterface

FunctionalInterface用法

FunctionalInterface关键字是放在接口中的注解,用于标示此接口可用于函数式编程形式 如”() ->{}“ 的写法,形式如下

@FunctionalInterfacepublic class Flyer{String fly(String name);}
@FunctionalInterface只能用在中只有一个虚方法的接口中,但接口中可以有其他有某认实现的方法或者静态方法

如下所示

@FunctionalInterfacepublic interface Flyer {String fly(String name);default int flyMaxHight(){return 1000;}static void flyMaxDistance(){System.out.println("最远里程");}}

但是@FunctionalInterface不能放在有一个以上虚方法的接口中,会报编译错误,如下所示


@FunctionalInterfacepublic interface Flyer {//报编译错误String fly(String name);void getSpeed();default int flyMaxHight(){return 1000;}static void flyMaxDistance(){System.out.println("最远里程");}}

FunctionalInterface举例

定义

/** * 飞行器 * @author 734621 * */@FunctionalInterfacepublic interface Flyer {String fly(String name);default int flyMaxHight(){return 1000;}static void flyMaxDistance(){System.out.println("最远里程");}}
测试
public class TestFlyer {@Testpublic void testPlane(){//调用plane方法plane((name)->{//函数式编程return "我是"+name;});
                // 或者
plane(name-> "我是"+name );}private void plane(Flyer flyer) {System.out.println(flyer.fly("飞机"));}}

Predicate&Consumer

Predicate 

Predicate的官方解释是”Determines if the input Object  matches for some criteria“,意思是判断输入的参数是否匹配某些条件,即先根据输入的参数及匹配条件判断,参数和匹配条件都是从参数中传入。Predicate带有泛型数据。

Consumer

Consumer的官方解释是”A Operation which accepts a single input argument  and returns no result, Unlike most other functional interfaces,Consumer is expected to operate via side-effects “ 意思是Consumer接收一个简单的参数并且不返回结果,不像其他函数式接口,Consumer期望执行带有副作用的操作。这一点与Function的apply不一样,Function的apply有返回值

Predicate&Consumer 例子

Predicate 和Consumer一般一起配合使用,例子如下

例子中定义了 衣服的类和客户的类,衣服有原价,客户买衣服根据客户的等级可以进行折扣,如下所示

package com.sf.simba.function.consumer;/** * 衣服类 * @author 734621 * */public class Clothes {private Double price;//衣服价格private String name;//衣服名称   public Clothes(String name,Double price){this.name = name;this.price = price;System.out.println(name + "\r\n原价:" + price + "元");}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}}
package com.sf.simba.function.consumer;/** * 客户类 * @author 734621 * */public class Customer {private String name;private int level;//用户等级VIPprivate Double freaCount = 0.0;//折扣private Clothes clothes;public Customer(String name,int level,Clothes clothes){this.name = name;this.level = level;this.clothes = clothes;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public Double getFreaCount() {return freaCount;}public void setFreaCount(Double freaCount) {this.freaCount = freaCount;}public Clothes getClothes() {return clothes;}public void setClothes(Clothes clothes) {this.clothes = clothes;}/** * 计算折扣后的价格 * 折扣后的价格=衣服原价 - 客户折扣*衣服原价 * 如折扣率为30表示可以折扣30%,即折扣后的价格为:  衣服原价*70% * @return */public Double getTruePrice(){Double result = 0D;result = clothes.getPrice() - ((getFreaCount()*clothes.getPrice())/100);System.out.println(getName() + ",根据你的等级,"+clothes.getName()+"打折后的价格是:" + result + "元");return result;}}

package com.sf.simba.function.consumer;import java.util.function.Consumer;import java.util.function.Predicate;import org.junit.Test;/** * 买衣服的服务类 * @author 734621 * */public class TransactionService {/** * 客户买衣服 * @param customer * @param predicate * @param consumer * @return */public Customer buyClothes(Customer customer,Predicate<Customer> predicate,Consumer<Customer> consumer){if(predicate.test(customer)){//判断客户的等级是否达到折扣的条件 如果达到了就可以折扣consumer.accept(customer);//打折扣}return customer;}@Testpublic void testBuyClothes(){Clothes clothes = new Clothes("羽绒服",1000D);//定义羽绒服,原价为1000Customer customers1 = new Customer("Simba",6,clothes);customers1 = buyClothes(customers1,customer -> customer.getLevel()>=5,//等级大于等于5才有折扣customer -> customer.setFreaCount(20D));//折扣是20%customers1.getTruePrice();//获得最好的价格}}

最后输出:
羽绒服
原价:1000.0元
Simba,根据你的等级,羽绒服打折后的价格是:800.0元











原创粉丝点击