catalogue
2, How to understand functional interfaces
3, Java has four built-in core functional interfaces
1, Definition
If only one abstract method is declared in an interface, the interface is called a functional interface.
The object of this interface can be created through lambda expression (if lambda expression throws a checked exception, the exception needs to be declared on the abstract method of the target interface)
Lambda expressions are essentially instances of functional interfaces
Custom functional interface
We can use @ FunctionalInterface annotation on an interface to check whether it is a functional interface. At the same time, javadoc will also contain a declaration that this interface is a functional interface
@FunctionalInterface public interface MyInterface { void method1(); }
2, How to understand functional interfaces
Java has been advocating "everything is object" since its birth. In Java, object-oriented (OOP) programming is everything. However, with the rise of python, scala and other languages and the challenges of new technologies, Java has to make adjustments to support a wider range of technical requirements, that is, Java can support not only OOP but also OOF (function oriented programming)
In functional programming languages, functions are treated as first-class citizens. In programming languages that treat functions as first-class citizens, the type of Lambda expression is function. But in Java 8, it's different. In Java 8, Lambda expressions are objects, not functions. They must be attached to a special object type - functional interface
Simply put, in Java 8, Lambda expressions are instances of functional interfaces. This is the relationship between Lambda expressions and functional interfaces. That is, as long as an object is an instance of a functional interface, the object can be represented by a Lambda expression
Therefore, those previously expressed by anonymous implementation classes can now be written in Lambda expressions
3, Java has built-in four core functional interfaces
Functional interface | Parameter type | Return type | purpose |
---|---|---|---|
Consumer<T> Consumer interface | T | void | Apply operations to objects of type T, including methods: void accept (T) |
Supplier<T> Supply type interface | nothing | T | Returns an object of type T, including the method: T get() |
Function<T,R> Functional interface | T | R | Applies an operation to an object of type T and returns a result. The result is an object of type R. Include method: R apply (T) |
Predicate<T> Deterministic interface | T | boolean | Determines whether an object of type T satisfies a constraint and returns a boolean value. Including method: boolean test (T) |
@Test public void test1(){ happyTime(500, new Consumer<Double>() { @Override public void accept(Double aDouble) { System.out.println("It's tiring to study. The price of buying some delicious food is" + aDouble); } }); System.out.println("**********************"); happyTime(400,money -> System.out.println("It's tiring to study. The price of buying some delicious food is" + money)); } public void happyTime(double money, Consumer<Double> con){ con.accept(money); } @Test public void test2(){ List<String> list = Arrays.asList("Beijing","Nanjing","Tokyo","the Western Capital"); List<String> filterStrs = filterString(list, new Predicate<String>() { @Override public boolean test(String s) { return s.contains("Beijing"); } }); System.out.println(filterStrs); System.out.println("*************************"); List<String> filterStrs1 = filterString(list,s -> s.contains("Beijing")); System.out.println(filterStrs); } //Filter the strings in the collection according to the given rules. This rule is determined by the predict method public List<String> filterString(List<String> list, Predicate<String> pre){ ArrayList<String> filterList = new ArrayList<>(); for (String s : list){ if(pre.test(s)){ filterList.add(s); } } return filterList; }