Introduction to Lambda Expressions--Functional Programming and Functional Interface

One, functional programming:

  1. Functional programming is a programming method based on functional interfaces and expressed by lambda
  2. The concept of functional programming is to substitute the code as reusable data into the program. (In the past, traditional object-oriented development used some specific information such as names and numbers. The core of functional programming is to use the prepared code as Reusable resources participate in the running process of the program, showing different behaviors by passing in different code blocks)
  3. Functional programming emphasizes "what do you want to do" instead of "how do you want to do it"

Two, functional interface:

  1. Functional interface: an interface with only one abstract method in the interface
  2. There are a lot of functional interfaces in java, such as java.lang.Runnable
  3. After jdk8, a series of new functional interfaces are provided, located in java.util.function

Three, commonly used functional interface

One, Predicate functional interface

  1. predicate is a new functional interface located in java.util.function
  2. predicate is used to test whether the incoming data meets the judgment requirements, similar to if, it returns true if the condition is met, otherwise it returns false
  3. The predicate interface needs to implement the test() method for logical judgment
public static void main(String[] args) {
    
    
    // 求小于4的数
    Predicate<Integer> predicate = n -> n < 4;
    System.out.println(predicate.test(12));
    }
public class PredicateSample {
    
    
    public static void filter(List<Integer> list, Predicate<Integer> predicate) {
    
    
        for (Integer i : list) {
    
    
            if (predicate.test(i)) {
    
    
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
    
    
        // 求奇数
        List<Integer> lists = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        filter(lists, n -> n % 2 == 1);
        // 求大于4的偶数
        filter(lists, n -> n % 2 == 0 && n > 4);
    }
}

Second, Consumer functional interface

Consumer, corresponding to a function code with no input parameters, no need to return data

1,包含Consumer,DoubleConsumer,IntConsumer,LongConsumer等

2. Method: accept() without any return value

public class ConsumerSample {
    
    
    public static void input(Consumer<String> consumer) {
    
    
        consumer.accept("测试啦啦啦~");
    }

    public static void main(String[] args) {
    
    
        input(s -> {
    
    
                    System.out.println("向控制台输入:" + s);
                }
        );
    }
}

Three, Function functional interface

Function<T,R>: Corresponding to the function code that has an input parameter and needs to return data

T represents the type of parameter to be passed in, R represents the type of returned data

Method : apply();

/**
 * function生成随机定长字符串
 */
public class FunctionSample {
    
    
    public static void main(String[] args) {
    
    
        Function<Integer, String> randomString = n -> {
    
    
            String str = "abcdefg123456";
            // 在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,
            // 而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            for (int i = 0; i < n; i++) {
    
    
                // 生成随机数
                int position = random.nextInt(str.length());
                // 追加
                sb.append(str.charAt(position));
            }
            return sb.toString();
        };
        // function核心方法
        System.out.println(randomString.apply(13));
    }
}

Four, attention

Functional interfaces only support one parameter at most. If multiple interfaces are needed, we need to write them manually. After we finish writing, we can add @FunctionalInterface annotations to inform the compiler that this is a functional interface and perform abstract method checks.

Five, functional programming and object-oriented programming comparison
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/112056396