Introduction to the

  • Object orientation is processing data and passing values
Peope people = new People(int i,String name);// The most typical constructors are passing values
public int sum(int a,int b){// Pass a value,
  return a+b;
}
Copy the code
  • A function is a behavior that can transfer behavior

  • The shape of the Lambda

(parm1,parm2,...) -> {method body}Copy the code

Why Lambda expressions are needed:

  • Prior to Java 8, we could not pass a function as an argument to a method or declare a method that returned a function
  • Lambda expressions are added to implement functions as arguments
  • In JavaScript, it is quite common for a function argument to be one function and a return value to be another function;
  • JavaScript is a very typical functional language
  • The classic callback
$("button").click(function(){// The callback() method passed in the click method
    $("p").hide(1000);
});
Copy the code

@FunctionalInterfaceBreak down

Let’s start with traversal

public class TraverseTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1.2.3.4.5.6);
        for (int i = 0; i < list.size(); i++){// Ancient times
            System.out.println(list.get(i));
        }
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        for (Integer i : list){// Modern -- iterator
            System.out.println(i);
        }
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
        /* Most functional interfaces just mean - functional interfaces, which strictly means that they take input, do some calculations, and return output. They should not change any state. Consumer is an exception because it does not return any value. Its purpose is simply to modify some state. * /
        list.forEach(new Consumer<Integer>() {/ / modern - forEach
            @Override
            public void accept(Integer integer) {
                int i = integer*integer;/ / side effects
                System.out.println(i);
            }
        });
        list.forEach(integer -> { System.out.println(integer);});
        list.forEach(System.out::println);// Method reference, which refers to the println method in the System.out object to implement the accept method}}Copy the code

Elements of perceived

What is meant byforEach?

  • Click on the source code and have a look

  • Information collection
    • forEach()is可迭代interfaceMethod in,There is already a default implementation
    • Function: Operates on each element in an iterator
    • Argument: Consumer, the action to be performed for each element

What is meant byConsumer?

  • Click on the source code to take a look

  • Information collection
    • ConsumerIt’s an interface, and it’s afunctional interface
    • There’s an Accept () method in there
    • It also says that unlike other functional interfaces, it operates through side effects
    • This is different because functions generally return values, whereas Consumer does not. If it doesn’t change and manipulate the data through side effects, then Consumer has no meaning. What does Consumer do? Side effects are simply changes and manipulation of data.
    • Details of side effects
    • Annotations are also used@FunctionalInterface

What is meant by@FunctionalInterface

  • Click on the source code and have a look

  • Information collection
    • Is an informational annotation designed to tell the compiler that this is a functional interface

    Functional interfaces have been translated as functional interfaces

    • A functional interface can have only one abstract method
    • Functional interfaces can have multiple default methods that are already implemented, or methods that are already implemented by other classes, such as toString() of the Object class.
      @FunctionalInterface
      public interface Function {
        void test(a);// There is only one abstract method
        default void test1(a){
          System.out.println(22222);
        }
        String toString(a);// Already implemented by Object, which the interface inherits, because Object is the parent of all classes
      }
    Copy the code
    • Functional interfaces can be instantiated through Lambda expressions, method references, or constructor references
    • As long as it conforms to the definition of a functional interface, with or without annotations@FunctionalInterface, the compiler can recognize

summary

Lambda expressions can be used to implement functional interfaces

list.forEach(integer -> { System.out.println(integer); });// Generally, you do not need to specify the parameter type
// Because List
      
        is sufficient for the compiler to infer the parameter type
      
Copy the code

The Consumer interface is implemented

2. A functional interface has only one abstract method. Implementing an abstract method also implements the interface. So when the argument to a method is a functional interface, you simply pass in the implementation of the method, which can be Lambda expressions and method references (rip-off methods from other classes).

3. Although a Lambda expression is a function, it is an object in Java because it is attached to a specific object type, the functional interface. Because it implements an abstract method in the corresponding functional interface, or rather it implements the entire interface, Lambda expressions are the implementation class of the interface

A small test

@FunctionalInterface
public interface Function {
    void test(a);
}
class myTest {
    public void FunctionalInter(Function function){
        function.test();
        System.out.println("The argument is that the functional interface is implemented.");
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
    }
    public static void main(String[] args) {
         myTest myTest = new myTest();
         myTest.FunctionalInter(() -> System.out.println("Expression has no arguments"));// Call test() indirectly
         Function functionInterface = () -> System.out.println("Assign an expression directly to the interface, and the interface has an implementation.");
         functionInterface.test();// Call test()}}Copy the code

You might wonder

What is the principle of iterators?

  • Design patterns in a nutshell — iterator patterns

What is a method reference?

Just a quick note

  • Format:
ClassName::Method
Copy the code
  • meaning
    • In order toforEachAs an example
    list.forEach(System.out::println);
    Copy the code
    • becauseforEachThe argument to is a functional interfaceConsumerThere are abstract methods in thereacceptMethod, as long as it is implementedacceptMethod implements the interfaceConsumerAny implementation? plagiarismSystem.outIn theprintlnmethods
    • To formaccept = println, which implements accept, which implements the Consumer interface