New functional interface with new features in Java 8

Java8 type features

1. Java8 new feature Lambda expression

2. New functional interface added by new Java 8 features

3. Java8 new feature method reference and constructor reference


New functional interface with new features in Java 8

Introduction

Functional interface: When there is only one abstract method in the interface, there can be multiple non-abstract methods; you can use the annotation @FunctionalInterface to modify, so you can check whether it is a functional interface;

use

Since the use condition of Lambda expression must have functional support, it can be used in Lambda expression;

Functional interface before Java 8

Common ones are:

java.lang.Runnable
java.util.concurrent.Callable
java.util.Comparator
java.io.FileFilter

Four new functional interfaces in Java 8

1. Consumer<T>: Consumer interface

      void accept(T t); //Accept an input parameter and no return operation

application

public static void main(String[] args) {
    happy(100,(m) -> System.out.println("买衣服花了" + m + "元"));
}

private static void happy(double money, Consumer<Double> consumer) {
   consumer.accept(money);
}

operation result: 

I spent 100 yuan on clothes

2.Supplier<T>: Supply interface

      T get(); //No parameters, return a result.

Case: Random field generates a data and saves it in the collection


    public static void main(String[] args) {
        List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
        for (Integer num : numList) {
            System.out.println(num);
        }
    }

    //产生一个随机数,并存储在List中
    private static List<Integer> getNumList(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

operation result: 

21
78
96
23
61
33
69
69
94
85

3. Function<T,R>: functional interface

      R apple(T t); //Accept an input parameter and return a result.

Case: Handling strings


    public static void main(String[] args) {
        String result = getStr("  hello   world", (x) -> x.replaceAll(" ", ""));
        System.out.println(result);

    }

    private static String getStr(String s, Function<String, String> function) {
        return function.apply(s);
    }

operation result:

helloworld 

4. Predicate<T>: assertion type interface

      boolean test(T t);//Accepts an input parameter and returns a boolean result.

Case: Put the String that meets the conditions into the collection

public static void main(String[] args) {
        List<String> list = Arrays.asList("android", "ios", "h5", "Java", "php", "python");
        List<String> strings = addStr(list, (s) -> s.length() > 3);
        for (String str : strings
        ) {
            System.out.println(str);
        }


        //不是Lambda表达式
        addStr(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length() > 3;
            }
        });
    }

    private static List<String> addStr(List<String> stringList, Predicate<String> predicate) {
        List<String> result = new ArrayList<>();

        for (String str : stringList) {
            if (predicate.test(str)) {
                result.add(str);
            }
        }
        return result;
    }

operation result:

android
Java
python

Of course, in addition to the above four categories, there are many, but they are all under the java.util.function package, which contains many categories to support functional programming in Java;

Guess you like

Origin blog.csdn.net/ezconn/article/details/108210138