栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Jdk8常见的函数式接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Jdk8常见的函数式接口

Comparator
public interface Comparator {
	int compare(T o1, T o2);
	}
public class ComparatorLambda {
    public static void main(String[] args) {
        String[] strs = {"kobe","james","jordan","durant"};
        //使用匿名内部类的方式实现比较器
        Comparator comparator = new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        };
//        Arrays.sort(strs,comparator);
//        System.out.println(Arrays.toString(strs));
        //TODO 用Lambda表达式实现
        Arrays.sort(strs,((o1, o2) -> o1.length() - o2.length()));
        System.out.println(Arrays.toString(strs));
    }
}

Consumer
public interface Consumer {
	void accept(T t);
	
	default Consumer andThen(Consumer after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
public class ConsumerLambda {
    public static void main(String[] args) {
//        consumerString(s -> System.out.println(s));
        consumerString(
                s -> System.out.println(s.toUpperCase()),
                s -> System.out.println(s.toLowerCase())
        );
    }
    public static void consumerString(Consumer fun){
        fun.accept("Hello");
    }
    public static void consumerString(Consumer first,Consumer sec){
        first.andThen(sec).accept("Hello");
    }
}

Function
public interface Function {
	R apply(T t);
	}
public class FunctionLambda {
    public static void main(String[] args) {
        method(s -> Integer.parseInt(s) + 10,
                integer -> integer *= 10);
    }
    static void method(Function one,Function two){
        int num = one.andThen(two).apply("10");
        System.out.println(num+20);
    }
}

Predicate
public interface Predicate {
	boolean test(T t);
	
	default Predicate and(Predicate other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
    
    default Predicate negate() {
        return (t) -> !test(t);
    }
    
    default Predicate or(Predicate other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

public class PredicateLambda {
    public static void main(String[] args) {
        andMethod(s -> s.contains("H"),s -> s.contains("w"));
        orMethod(s -> s.contains("H"),s -> s.contains("W"));
        negateMethod(s -> s.length() < 5);
    }
    static void andMethod(Predicate one,Predicate two){
        boolean flag = one.and(two).test("Helloworld");
        System.out.println("字符串符合要求吗:"+ flag);
    }
    static void orMethod(Predicate one,Predicate two){
        boolean flag = one.or(two).test("Helloworld");
        System.out.println("字符串符合要求吗:"+ flag);
    }
    static void negateMethod(Predicate one){
        boolean flag = one.negate().test("Helloworld");
        System.out.println("字符串很长吗:"+ flag);
    }
}

Runnable
public interface Runnable {
	public abstract void run();
	}
public class RunnableLambda {
    public static void main(String[] args) {
        //使用匿名内部类的方式,实现多线程
        new Thread(new Runnable() {
            public void run() {
                String name = Thread.currentThread().getName();
                System.out.println(name + " 线程已经启动");
            }
        }).start();

        //TODO 使用 Lambda表达式,实现多线程
        new Thread(
                () -> {
                    String name = Thread.currentThread().getName();
                    System.out.println(name + " 线程已经启动2");
                }
        ).start();
    }
}

Supplier
public interface Supplier {
	T get();
	}
public class SupplierLambda {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,555,8};
        int max = getMax(() -> {
            int temp = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (arr[i] > temp)
                    temp = arr[i];
            }
            return temp;
        });
        System.out.println(max);
    }
    public static int getMax(Supplier supplier){
        return supplier.get();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949215.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号