New features of Java Lambda expressions and the use of built-in functions

Use 1. Lambda expressions

Lambda is JDK1.8the introduction of important new features, the use of functional programming.
Functional programming and object-oriented programming can be understood as two development camps. Object-oriented biggest limitation is that: the structure must be complete . For example: writing a main method, you must create a package, write a class, define a main method, and then write the output statement.


For example: the conventional object-oriented development
public interface IMessage {
    void print();
}

class TestLambda{
    public static void main(String[] args) {
       IMessage m = new IMessage(){     //匿名内部类
           @Override
           public void print() {     //必须编写完整语法
               System.out.println("IMessage实现类");
           }
       } ;
       m.print();
    }
}

For the above, using a more simplified approach, functional programming.
Example: The functional programming

public interface IMessage {
    void print();
}

class TestLambda{
    public static void main(String[] args) {
        //函数式编程的使用,目的是输出一句话
        IMessage m = () -> System.out.println("IMessage实现类");
        m.print();        
    }
}

Functional programming premise : the interface must have only one way , if there are two methods can not be used. If there is now a function of the interface it is to programming born, so defined when only a method can be defined .
Functional programming using summary:

  1. Correspond to functional programming mathematics: Y = F (X), where f -> function;
  2. You can define an abstract interface method;
  3. In order to prevent an abstract method defined in the interface function than used when interface definitions @FunctionalInterface;
  4. Using lambda represents syntax:
    (parameter) -> single statement;
    (parameter) -> {}; {} is a multi-line statement;

Example: Use @FunctionalInterfaceannotations

//是一个函数式编程接口,只允许有一个方法
@FunctionalInterface
public interface IMessage {
    void print();
}

class TestLambda{
    public static void main(String[] args) {
       IMessage m = ()->{
            System.out.println("IMessage实现类");
            System.out.println("IMessage实现类");
        };
        m.print();
    }
}

Example: Calculation functional programming

@FunctionalInterface
interface IAdd{
    int add(int a, int b);
}
public class TestLambda {
    public static void main(String[] args) {
        //如果return中只有一个语句,则省略return
        IAdd iAdd = (a,b)-> a+b;
        System.out.println(iAdd.add(2,3));
    }
}

lambda Caution:

  • The lambda] Procedure:
	(参数名列表) -> {
		code1;
		code2;
		[return];
	}
  • If the method is only one statement in the body, and as a return value, omit return ;
  • If the body of the method is only one statement, you can save {} `;
  • Caution example:
public class TestLambda {
    public static void main(String[] args) {

        //1.  int a = 0;  错误,参数列表中的变量名称不能和外部代码变量重名
        int c = 2;
        IAdd iAdd = (a,b)-> {
            //2. c = 0; 错误
            //如果方法体中访问外部变量,隐式被final修饰,不能被修改
            return a+b;
        };
        System.out.println(iAdd.add(2,3));
    }
}

2. Built-function interface

The core is characterized in lambda: the function interface . The core function of the interface: only one way .
java.util.function functional programming into the following four interfaces:

  1. Function functional interface: public interface Function <T, R> ---- R apply (T t);
  2. Type supply function interface: public interface Supplier ---- T get ();
  3. Function consumer interface: public interface Consumer ---- void accept (T t);
  4. Interface asserts: public interface Predicate ---- boolean test (T t);

They are internal JDK functions provided.


2.1 Functional Interface

Functional means you input data , and after data processing after output .
Here Insert Picture Description
Example: Use of the interface


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

        /*
        Function<Integer,String> function = (t)->{
            return String.valueOf(t);
        };*/
        Function<Integer,String> function = String::valueOf;
        System.out.println(function.apply(5));  //5
    }
}

2.2 Interface supply

The purpose is to obtain a result.
Here Insert Picture Description
Example: Use of the interface

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

        String str = "hello";
        /*
        Supplier<String> supplier = ()->{
            return str.toUpperCase();
        };*/
        
        Supplier<String> supplier = str::toUpperCase;
        System.out.println(supplier.get());
    }
}

2.3 Consumer Interface

Perform certain operations on the given parameters.
Here Insert Picture Description
Example: Use of the interface


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

        /*
        Consumer<String> consumer = (t)->{
            System.out.println(t);
        };*/
        Consumer<String> consumer = System.out::println;
        consumer.accept("hello");    //hello

        Consumer<Integer[]> consumer1 = (arrays)->{
            for (Integer array:arrays) {
                System.out.print(array+",");
            }
        };
        consumer1.accept(new Integer[]{1,2,3});
    }
}

2.4 Assertion Interface

Mainly used for judgment.
Here Insert Picture Description
Example: Use of the interface

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

        String str = "JavaScript";
        Predicate<String> predicate = str::startsWith;
        System.out.println(predicate.test(str));  //true
        
    }
}

Guess you like

Origin blog.csdn.net/mi_zhi_lu/article/details/91366210