JAVA8函数式编程应用

Java8 引入了函数式编程,可以把函数作为参数传入实现通用方法。熟知的 Java 8 单个参数函数式接口比如 Predicate、Consumer、Supplier、Function。

接下来我们自定义四个与之功能相似的函数式接口来进行应用

@FunctionalInterface
public interface PredicateInterface<T> {
    boolean test(T t);
}

@FunctionalInterface
public interface ConsumerInterface<T> {
    void accpet(T t);
}

@FunctionalInterface
public interface FunctionInterface<T,R> {
    R apply(T t);
}

@FunctionalInterface
public interface SupplierInterface<T> {
    T get();
}

新建需要用到的实体类

/**动物类*/
public class Animal {

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    private int age;

}





/**人类*/
public class People {

    public People() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    private int age;
}

创建函数式接口的通用方法类

package test;

import test.service.ConsumerInterface;
import test.service.FunctionInterface;
import test.service.PredicateInterface;
import test.service.SupplierInterface;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.Function;

public class CommonMethods<T> {

    public static <T> List<T> PredicateImpl(List<T> list, PredicateInterface<T> predicate){
        if (list.isEmpty()){
            return new ArrayList<>(0);
        }
        List<T> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            T t = list.get(i);
            if (predicate.test(t)){
                resultList.add(t);
            }
        }
        return resultList;
    }

    public static <T> List<T> ConsumerImpl(List<T> list, ConsumerInterface<T> consumer){
        if (list.isEmpty()){
            return new ArrayList<>(0);
        }
        for (int i = 0; i < list.size(); i++) {
            consumer.accpet(list.get(i));
        }
        return list;
    }

    public static <T> List<T> SupplierImpl(List<T> list, SupplierInterface<T> supplier){
        T t = supplier.get();
        list.add(t);
        return list;
    }

    public static <T,R> R FunctionImpl(T Object, FunctionInterface<T,R> function){
        R apply = function.apply(Object);
        return apply;
    }

}

最后编写我们的测试类

package test;


import test.entity.Animal;
import test.entity.People;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Java8内置的函数式编程接口应用场景和方式
 */
public class CommonMethodsTest {

    public static void main(String[] args) {

        List<Animal> animalPredicate = new ArrayList<>(Arrays.asList(new Animal("兔子", 2), new Animal("鸭子", 20), new Animal("熊猫", 200)));
        List<Animal> animalPredicaterResult = CommonMethods.PredicateImpl(animalPredicate, animal -> animal.getAge() > 100);
        animalPredicaterResult.forEach(System.out::println);
        //Animal{name='熊猫', age=200}

        List<Animal> animalConsumer = Arrays.asList(new Animal("兔子", 2), new Animal("鸭子", 20), new Animal("熊猫", 200));
        List<Animal> animalConsumerResult = CommonMethods.ConsumerImpl(animalConsumer, animal -> animal.setAge(2000));
        animalConsumerResult.forEach(System.out::println);
        //Animal{name='兔子', age=2000}
        //Animal{name='鸭子', age=2000}
        //Animal{name='熊猫', age=2000}

        List<Animal> animalSupplier = CommonMethods.SupplierImpl(new ArrayList<>(), () -> new Animal("兔子", 2));
        animalSupplier.forEach(System.out::println);
        //Animal{name='兔子', age=2}

        People peopleFunction = CommonMethods.FunctionImpl(new Animal("兔子", 2), (Animal animal) -> new People(animal.getName(), animal.getAge()));
        System.out.println(peopleFunction);
        //Animal{name='兔子', age=2}



        //Function拓展
        Function<Animal, People> testToMyTest = (t) -> {
            People people = new People();
            people.setAge(t.getAge());
            people.setName(t.getName());
            return people;
        };
        //集合对象转集合对象
        List<Animal> tests = new ArrayList<>(Arrays.asList(new Animal("兔子", 2), new Animal("鸭子", 20), new Animal("熊猫", 200)));
        List<People> myTests = tests.stream()
       .map(testToMyTest)
       .collect(Collectors.<People> toList());
        //单个
        Animal test = new Animal("兔子", 2);
        People myTest = testToMyTest.apply(test);
        System.out.println(myTest);
        //function操作可优化
        List<People> collect = tests.stream().map((t) -> {
            People people = new People();
            people.setAge(t.getAge());
            people.setName(t.getName());
            return people;
        }).collect(Collectors.toList());


    }

}


Java 8 中函数式接口列表

现在我们给出一份较为全的函数式接口与描述符对应的接口声明列表:

    函数式接口    函数描述符
Predicate<T>  (T)  -> boolean
Consumer<T>  (T)  -> void
Function< T, R >  (T)  -> R
Supplier<T>  ( )  -> T
UnaryOperator<T>   (T)  ->  T
BinaryOperator<T>  (T, T) -> T
BiPredicate<L, R>  (L, R)  -> boolean
BiConsumer<T, U>  (T, U)  -> void
BiFunction<T, U, R>  (T, U)  -> R
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值