Let's learn Java8 together (1) - functional programming

In this article, we will learn how to do functional programming in Java 8.

functional programming

The so-called functional programming is to pass the function name as a value, and then the receiver gets the function name and calls it.

Let's first look at how JavaScript makes function calls

var Person = {
    sayHello: function(name) {
        alert('hello ' + name);
    }
};

function work(fn) {
    fn('Jim');
}

work(Person.sayHello); // hello Jim

In this example, the sayHellofunction is passed as an argument to another workfunction, and work calls the given function.

Functional Programming in Java 8

In Java, the role of a function is a method in a class, and the function mentioned in this article generally refers to a method.

Let's take a look at a simple functional programming example in Java 8:

import java.util.function.Consumer;

class Person {
    public static void sayHello(String name) {
        System.out.println("hello " + name);
    }
}

public class TestPerson {

	public static void main(String[] args) {
		work(Person::sayHello); // hello Jim
	}
	
	public static void work(Consumer<String> consumer) {
		consumer.accept("Jim");
	}
}

As can be seen from this example, the way of passing functions in Java8 is different from that in JavaScript. The way of passing functions in Java8 is: 方法引用::方法名称, such as: String.valueOf, String.toString. You don't need to understand the receiving parameter Consumer first, which will be discussed later.

In order to receive the passed function reference, the Java designers proposed the 函数式接口concept of a

functional interface

Functional interface definition: There is only one in an interface 抽象方法.

The following interfaces are functional interfaces:

  • Consumer
  • Supplier
  • Runnable
  • Callable

Looking at the source code of the Runnable class, you can see that an @FunctionalInterfaceannotation is defined above the class

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
  • The role of the @FunctionalInterface annotation:

When used as a marker, it marks the current interface as a functional interface. If your interface has multiple abstract methods or no abstract methods, it will compile an error. Its role is somewhat similar to @overrideannotations, with auxiliary functions, but not required. If you want to design a functional interface, then it's better to add it.

// 编译不通过,必须要有一个抽象方法
@FunctionalInterface
public interface InterfaceA {
	
}

// 编译通过,只有一个抽象方法
@FunctionalInterface
public interface InterfaceB {
	void run();
}

// 编译不通过,只能有一个抽象方法
@FunctionalInterface
public interface InterfaceC {
	void run();
	
	void run2();
}

// 编译通过,run2是默认方法,并不是抽象方法
// 在这里只有一个run抽象方法
@FunctionalInterface
public interface InterfaceD {
	void run();
	
	default void run2() {};
}

The interface default method is involved in the example InterfaceD default void run2() {};, which is omitted here and will be mentioned in subsequent articles.

We can implement a custom functional interface instead of Consumer

// 自定义函数式接口
@FunctionalInterface
public interface FunctionCaller<T> {
	void call(T t);
}

public class TestPerson2 {

	public static void main(String[] args) {
		work(Person::sayHello);
	}
	
	public static void work(FunctionCaller<String> caller) {
		caller.call("Jim");
	}
}

summary

This article mainly explains how to perform functional programming under Java8, and how to define and use a functional interface.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324142762&siteId=291194637