java8 - functional interface

New feature of java8-functional interface

Function interface: an interface and an abstract method constitute a functional interface, which can be annotated @FunctionalInterface 

                   Specify the constraint that the interface is a functional interface; a functional interface can only have one abstract method of its own

Simple to use:

Custom function interface: Three function interfaces are defined

package com.xiaodu.java8.methodcite;

import java.util.function.Supplier;

/**
 * 定义函数接口
 * @author 84287
 *
 */
public interface FunctionInterface {
		
	
	/*
	 * 获取类的实例
	 */
	@FunctionalInterface
	interface function01<T>{
		 T getObject();
	}
	//函数接口是主要是给Lambda使用 java8中已经有好多函数接口,很多不用自己定义

      //本人自己定义的function01<t> 其实和 java.util.function中的Supplier<T>一样的

	
}

 

Use function interface:

public static void main(String[] args) {
		
		function01<String> f01 = () -> "xiaoming";
		String string = f01.getObject();
		System.out.println(string.length());  // 8
	}

Complete list of function interfaces included in java 8:

Functional interfaces that existed before JDK 1.8:

·        java.lang.Runnable

·        java.util.concurrent.Callable

·        java.security.PrivilegedAction

· Java.util.Comparator

Java.io.FileFilter

Java.nio.file.PathMatcher

·        java.lang.reflect.InvocationHandler

·        java.beans.PropertyChangeListener

·        java.awt.event.ActionListener

·        javax.swing.event.ChangeListener
--------------------- 

Function interface under java.util.function.*

Function interface
Serial number Interface name && abstract method Remarks
1

BiConsumer<T,U>   ;

 void accept(T t, U u);

Represents an operation that accepts two input parameters and does not return any results
2

BiFunction<T,U,R>;

  R apply(T t, U u);

Represents a method that accepts two input parameters and returns a result
3

BinaryOperator<T>;

继承BiFunction<T,U,R>;

Represents an operation that acts on two operators of the same type, and returns the result of the same type of operator
4

BiPredicate<T,U>;

 boolean test(T t, U u);

Represents a two-parameter return boolean value method
5 BooleanSupplier Represents the provider of the boolean value result

6

Consumer<T>;

  void accept(T t);

Represents an operation that accepts an input parameter and does not return
7

DoubleBinaryOperator;

 double applyAsDouble(double left, double right);

Represents the operation of two double value operators, and returns a double value result
8

DoubleConsumer;

 void accept(double value);

Represents an operation that accepts a double value parameter and does not return a result
9

DoubleFunction<R>;

  R apply(double value);

Represents a method that accepts a double value parameter and returns the result
10

DoublePredicate;

boolean test(double value);

Represents a boolean return method with a double value parameter
11

DoubleSupplier;

 double getAsDouble();

Represents a method that returns a double value without parameters
12

DoubleToIntFunction;

 int applyAsInt(double value);

Accept a double type input and return an int type result
13

DoubleToLongFunction;

 long applyAsLong(double value);

Accept a double type input and return a long type result
14

DoubleUnaryOperator;

double applyAsDouble(double operand);

Accepts a parameter of the same type double, and the return value type is also double.
15

Function<T,R>;

  R apply(T t);

接受一个输入参数,返回一个结果
16

IntBinaryOperator;

 int applyAsInt(int left, int right);

接受两个参数同为类型int,返回值类型也为int 
17

IntConsumer;

void accept(int value);

接受一个int类型的输入参数,无返回值
18

IntFunction<R>;

 R apply(int value);

接受一个int类型输入参数,返回一个结果
19

IntPredicate;

boolean test(int value);

接受一个int输入参数,返回一个布尔值的结果
20

IntSupplier;

int getAsInt();

无参数,返回一个int类型结果
21

IntToDoubleFunction;

 double applyAsDouble(int value);

接受一个int类型输入,返回一个double类型结果
22

IntToLongFunction;

  long applyAsLong(int value);

接受一个int类型输入,返回一个long类型结果
23

IntUnaryOperator;

 int applyAsInt(int operand);

接受一个参数同为类型int,返回值类型也为int 
24

LongBinaryOperator;

long applyAsLong(long left, long right);

接受两个参数同为类型long,返回值类型也为long
25

LongConsumer;

void accept(long value);

接受一个long类型的输入参数,无返回值
26

LongFunction<R>;

 void accept(long value);

接受一个long类型输入参数,返回一个结果
27

LongPredicate;

 boolean test(long value);

接受一个long输入参数,返回一个布尔值类型结果
28

LongSupplier;

long getAsLong();

无参数,返回一个结果long类型的值
29

LongToDoubleFunction;

 double applyAsDouble(long value);

接受一个long类型输入,返回一个double类型结果

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

30

LongToIntFunction;

int applyAsInt(long value);

接受一个long类型输入,返回一个int类型结果
31

LongUnaryOperator;

 long applyAsLong(long operand);

接受一个参数同为类型long,返回值类型也为long
32

ObjDoubleConsumer<T>;

 void accept(T t, double value);

接受一个object类型和一个double类型的输入参数,无返回值
33

ObjIntConsumer<T>;

  void accept(T t, int value);

接受一个object类型和一个int类型的输入参数,无返回值
34

ObjLongConsumer<T>;

  void accept(T t, long value);

接受一个object类型和一个long类型的输入参数,无返回值
35

Predicate<T>;

 boolean test(T t);

接受一个输入参数,返回一个布尔值结果
36

Supplier<T>;

T get();

无参数,返回一个结果
37

ToDoubleBiFunction<T,U>;

 double applyAsDouble(T t, U u);

接受两个输入参数,返回一个double类型结果
38

ToDoubleFunction<T>;

  double applyAsDouble(T value);

接受一个输入参数,返回一个double类型结果
39

ToIntBiFunction<T,U>;

 int applyAsInt (T t, U u);

Accepts two input parameters and returns an int type result
40

ToIntFunction<T>;

  int applyAsInt(T value);

Accepts an input parameter and returns an int type result
41

ToLongBiFunction<T,U>;

  long applyAsLong(T t, U u);

Accepts two input parameters and returns a long type result.
42

ToLongFunction<T>;

  long applyAsLong(T value);

Accepts an input parameter and returns a long type result
43

UnaryOperator<T>;

继承: extends Function<T, T>;

 R apply(T t);

Accepts a parameter of type T, and the return value type is also T
44 The above is the functional interface after java8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Simply use the function interface that comes with java8

The code reflects:

 

 

 

 

 

 

 

 

 

 

 

 

 

Use the functional interface that comes with java8

 

	public static void main(String[] args) {
		
		String string = MethodCiteFunction.supplier(() -> "接受一个空参,返回一个String对象");
		System.out.println(string);
//		空参,返回一个person对象
		Person person = MethodCiteFunction.supplier(() -> new Person());
//		方法引用,空参返回一个person对象
		Person person2 = MethodCiteFunction.supplier(Person::new);
		
		
	}
	
	/**
	 * Supplier<T>  无参,返回一个泛型类型的对象
	 * @param supplier
	 * @return
	 */
	public static <T>T supplier(Supplier<T> supplier) {
		return  supplier.get();
	}

 

Guess you like

Origin blog.csdn.net/xiaodujava/article/details/84316944