1、Runnable
public class App {
public void text ( int n, Runnable runnable) {
for ( int i = 0 ; i < n; i++ ) {
runnable. run ( ) ;
}
}
public void data ( ) {
System . out. println ( "woaini" ) ;
}
public static void main ( String [ ] args) {
new App ( ) . text ( 10 , ( ) -> System . out. println ( "tangxin" ) ) ;
Runnable runnable = new App ( ) :: data ;
runnable. run ( ) ;
}
}
2、Supplier
class Persons {
private int age;
public void setAge ( int age) {
this . age = age;
}
public int getAge ( ) {
return this . age;
}
}
public class App {
public Persons text ( Supplier < Persons > supplier) {
return supplier. get ( ) ;
}
public static void main ( String [ ] args) {
Persons persons = new App ( ) . text ( Persons :: new ) ;
persons. setAge ( 100 ) ;
System . out. println ( persons. getAge ( ) ) ;
Supplier < Persons > supplier = Persons :: new ;
Persons persons1 = supplier. get ( ) ;
persons1. setAge ( 99 ) ;
System . out. println ( persons1. getAge ( ) ) ;
}
}
3、Consumer
import java. util. function. Consumer ;
class Persons {
private int age;
public void setAge ( int age) {
this . age = age;
}
public int getAge ( ) {
return this . age;
}
}
public class App {
public void text ( int age) {
System . out. println ( age) ;
}
public void text1 ( Persons persons) {
persons. setAge ( 12 ) ;
System . out. println ( persons. getAge ( ) ) ;
}
public static void main ( String [ ] args) {
Consumer < Integer > consumer = new App ( ) :: text ;
consumer. accept ( 99 ) ;
Consumer < Persons > consumer1 = new App ( ) :: text1 ;
consumer1. accept ( new Persons ( ) ) ;
}
}
4、BiConsumer
import java. util. function. BiConsumer ;
class Persons {
private int age;
public void setAge ( int age) {
this . age = age;
}
public int getAge ( ) {
return age;
}
}
class Man extends Persons {
public void eat ( ) {
System . out. println ( "eat mutton" ) ;
}
}
public class App {
public void text ( Persons persons, Man man) {
persons. setAge ( 34 ) ;
System . out. println ( persons. getAge ( ) ) ;
man. eat ( ) ;
}
public static void main ( String [ ] args) {
BiConsumer < Persons , Man > biConsumer = new App ( ) :: text ;
biConsumer. accept ( new Persons ( ) , new Man ( ) ) ;
}
}
5、Function
import java. util. function. Function ;
public class App {
public String text ( int data) {
return Integer . toString ( data) ;
}
public static void main ( String [ ] args) {
Function < Integer , String > function = new App ( ) :: text ;
System . out. println ( function. apply ( 123 ) ) ;
}
}
6、BiFunction
import java. util. function. BiFunction ;
public class Demo {
public int text ( String data, String data1) {
if ( data. length ( ) > data1. length ( ) ) {
return 1 ;
}
else if ( data. length ( ) == data1. length ( ) ) {
return 0 ;
}
return - 1 ;
}
public static void main ( String [ ] args) {
BiFunction < String , String , Integer > biFunction = new Demo ( ) :: text ;
System . out. println ( biFunction. apply ( "tangxin" , "zhanggang" ) ) ;
}
}
7、UnaryOperator
import java. util. function. UnaryOperator ;
class Person {
private int age;
public void setAge ( int age) {
this . age = age;
}
public int getAge ( ) {
return this . age;
}
}
public class Demo {
public Person text ( Person person) {
person. setAge ( 99 ) ;
return person;
}
public static void main ( String [ ] args) {
UnaryOperator < Person > unaryOperator = new Demo ( ) :: text ;
Person person = unaryOperator. apply ( new Person ( ) ) ;
System . out. println ( person. getAge ( ) ) ;
}
}
8、BinaryOperator
import java. util. function. BinaryOperator ;
public class Demo {
public Integer text ( Integer data, Integer data1) {
data = data + data1;
return data;
}
public static void main ( String [ ] args) {
BinaryOperator < Integer > binaryOperator = new Demo ( ) :: text ;
System . out. println ( binaryOperator. apply ( 9 , 9 ) ) ;
}
}
9、Predicate
import java. util. function. Predicate ;
public class Demo {
public boolean text ( Integer data) {
if ( data > 100 ) return true ;
else return false ;
}
public static void main ( String [ ] args) {
Predicate < Integer > predicate = new Demo ( ) :: text ;
System . out. println ( predicate. test ( 90 ) ) ;
}
}