Java8六大新特性之二三 函数式接口函数默认方法和静态方法

函数式接口介绍

函数式接口中可以且只能有一个抽象方法,还可以有默认方法和静态方法,必须有@FunctionalInterface作为注解。

在java里面,lambda表达式需要函数是接口的承载,要不然没法实现。

下面自定义一个函数式接口的例子:

定义函数式接口TestFunInterface

@FunctionalInterface
public interface TestFunInterface{
    public void test();                          //可以有唯一一个抽象方法
    default String getName() { return "Allen"; } //可以有默认方法
    static String getGender() { return "Male"; } //可以有静态方法
}

使用该函数式接口TestFunInterface

class TestFunInterfaceImpl implements TestFunInterface {
    @Override
    public void test() {
        System.out.println( "test:" + getName());
    }
}
public class FuncInterfaceTest {
    public static void test(TestFunInterface tester){
        tester.test();
    }
    public static void main(String[] args) {
        // 1. 传入实例
        TestFunInterfaceImpl tester  = new TestFunInterfaceImpl();
        test(tester);
        // 2. 传入内部类
        test(new TestFunInterface(){
            @Override
            public void test() {
                System.out.println( "test:" + getName());
            }
        });
        //3. Lamadba表达式代替内部类
        test(()->System.out.println( "test:"));
    }
}

@FunctionalInterface对应的内容在java.lang.util.function包里面

function包里面的常用函数式接口

消息消费的函数式接口 - Consumer

唯一抽象方法为:

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

Consumer的使用实例:

import java.util.function.Consumer;
public class ConsumerTest {
    public static void main(String[] args) {
        consumer(something->{
            System.out.println(something + "welcome, ");
        },"hello message ");

        doubleConsumer(
                something-> System.out.println(something + "hello, "),
                something-> System.out.println(something + "welcome, "),
                "hello message ");

    }
    public  static  void consumer(Consumer<String> something, String message){
         something.accept(message);
    }
    public  static  void doubleConsumer(Consumer<String> something1, Consumer<String> something2, String message){
        something1.andThen(something2).accept(message);
    }
}

执行结果:

hello message welcome, 
hello message hello, 
hello message welcome, 

Process finished with exit code 0

消息提供的函数式接口 - Supplier

传入什么类型就返回什么类型

唯一抽象方法为:

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();

使用实例:

import java.util.function.Supplier;
public class SupplierTest {
    public static void main(String[] args) {
        int [] array  = {1,3,11,6,2,10};
        int a = getMax(()->{
            int m=0;
            for  (int i =0;i<array.length;i++) {
                if (i==0) m = array[0];
                if (array[i] > m) m= array[i];
            }
            return m;
        });
        System.out.println("the max value of array is: "+ a);

        System.out.println(getNewString(()->"hello"));

    }
    public  static  Integer getMax(Supplier <Integer> array){
        return array.get();
    }
    public  static  String getNewString(Supplier <String> string){
        return string.get() + " world!";
    }
}

执行结果:

the max value of array is: 11
hello world!

Process finished with exit code 0

判断条件的函数是接口 - Predicater

唯一的抽象方法为:

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

使用实例:

import java.util.function.Predicate;

public class PredicateTest {
    public static void main(String[] args) {
        String hasA = "a";
        String hasB = "b";

        boolean t1 = predicate("hello java",something-> something.contains(hasA));
        boolean t2 = negPredicate("hello java",something-> something.contains(hasA));
        boolean t3 =andPredicate("hello java",something-> something.contains(hasA),something-> something.contains(hasB));
        boolean t4 =orPredicate("hello java",something-> something.contains(hasA),something-> something.contains(hasB));

        System.out.println("t1 : " + t1);
        System.out.println("t2 : " + t2);
        System.out.println("t3 : " + t3);
        System.out.println("t4 : " + t4);
    }
    public  static  boolean predicate(String message, Predicate <String> something){
        return something.test(message);
    }
    public  static  boolean negPredicate(String message, Predicate <String> something){
        return something.negate().test(message);
    }
    public  static  boolean andPredicate(String message, Predicate <String> something1, Predicate <String> something2){
        return something1.and(something2).test(message);
    }
    public  static  boolean orPredicate(String message, Predicate <String> something1, Predicate <String> something2){
        return  something1.or(something2).test(message);
    }
}

执行结果:

t1 : true
t2 : false
t3 : false
t4 : true

Process finished with exit code 0

类型转换的函数时接口 - Function

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

function使用例子:

import java.util.function.Function;

public class FunctionTest {
    public static void main(String[] args) {

        int i = function("1000",something->Integer.parseInt(something));
        System.out.println(i);
        i = andFunction("1000",something->Integer.parseInt(something),something->something/100);
        System.out.println(i);
    }
    public  static  Integer function(String message, Function<String,Integer> something){
        return something.apply(message);
    }
    public  static  Integer andFunction(String message, Function<String,Integer> something1, Function<Integer,Integer> something2){
        return something1.andThen(something2).apply(message);
    }
}

执行结果:

1000
10

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/pengweismile/article/details/109693347