java lambda常用函数式接口

1、Runnable

/**
 * Runnable 参数类型:无   返回类型:void 抽象方法名:run
 * 描述:作为无参数或返回值的动作运行
 */

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

/**
 * Supplier<T> 参数类型:无 返回类型:无 抽象方法名:get
 * 描述:提供一个T类型的值
 */
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

/**
 * Consumer<T> 参数类型:T 返回类型:void 抽象方法名:accept
 * 描述:处理一个T类型的值
 * 其他方法:andThen()
 */
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;

/**
 * BiConsumer<T,U> 参数类型:T,U 返回类型:void 抽象方法名:accept
 * 描述:处理T和U类型的值
 * 其他方法:andThen()
 */
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;

/**
 * Function<T,R> 参数类型:T 返回类型:R 抽象方法名:apply
 * 描述:有一个T类型参数的函数
 * 其他函数:compose(), andThen(), identity()
 */

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

/**
 * BiFunction<T,U,R> 参数类型:T,U 返回类型:R 抽象方法名:apply
 * 描述:有T和U类型参数的函数
 * 其他函数:andThen()
 */

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;

/**
 * UnaryOperator<T> 参数类型:T 返回类型:T 抽象方法名:apply()
 * 描述:类型T上的一元操作符
 * 其他函数:compose(), andThen(), identity()
 */

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;
/**
 * BinaryOperator<T> 参数类型:T,T 返回类型:T 抽象方法名:apply()
 * 描述:类型T上的二元操作符
 * 其他函数:andThen(), maxBy(), minBy()
 */
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;

/**
 * Perdicate<T> 参数类型:T 返回类型:Boolean 抽象函数名:test()
 * 描述:布尔值函数
 * 其他函数名:and(), or(), negate(), isEqual()
 */
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));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值