java函数式接口

一、函数式接口

1、概述

 示例:

package com.itbianma09;

@FunctionalInterface
public interface MyInterface {
    void show();
}


package com.itbianma09;

public class Demo {
    public static void main(String[] args) {
        MyInterface my = () -> System.out.println("函数式接口");
        my.show();
    }
}

 

2、函数式接口作为方法参数

代码实现:

package com.bianma10;

public class RunnableDemo {
    public static void main(String[] args) {
        //匿名内部类方式
        startThread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"线程启动了");
            }
        });
        //函数式接口实现
        startThread(()-> System.out.println(Thread.currentThread().getName()+"线程启动了"));
    }
    private static void startThread(Runnable r){
        new Thread(r).run();
    }
}

 

3、函数式接口作为方法返回值

package itbianma01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

public class Demo {
    public static void main(String[] args) {
        ArrayList<String> array=new ArrayList<>();
        array.add("bbbbb");
        array.add("aaa");
        array.add("cccc");
        System.out.println("排序前:"+array);
        //Collections.sort(array);
        Collections.sort(array,getComparator());
        System.out.println("排序后:"+array);
    }
    private static Comparator<String> getComparator(){
        //匿名方法类实现
        /*return new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        };*/

        return (String s1,String s2)->s1.length()-s2.length();
    }
}

  

二、常见的函数式接口

 

1、Supplier接口

示例:

package itbianma01;

import java.util.ArrayList;
import java.util.function.Supplier;

public class Demo1 {
    public static void main(String[] args) {
        int[] arr = {1, 38, 5, 2, 4};
        int result = getMax(() -> {
            int max=arr[0];
            for(int i=1;i<arr.length-1;i++){
                if (max < arr[i]) {
                    max=arr[i];
                }
            }
            return max;
        });
        System.out.println(result);
    }

    private static Integer getMax(Supplier<Integer> sup) {
        return sup.get();
    }
}

 

2、Consumer接口

 

package itbianma01;

import java.util.ArrayList;
import java.util.function.Consumer;

public class Demo2 {
    public static void main(String[] args) {
        String[] array = {"小明,30", "小红,21", "小兰,22"};
        printInfo(array, s -> System.out.print(s.split(",")[0])
                , s -> System.out.println(s.split(",")[1])
        );
    }

    private static void printInfo(String[] array, Consumer<String> con1, Consumer<String> con2) {
        for (String s : array) {
            con1.andThen(con2).accept(s);
        }
    }
}

 

3、Predicate接口

package itbianma01;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class Demo2 {
    public static void main(String[] args) {
        String[] array = {"王小明,35", "小红,34", "孙小兰,22"};
        ArrayList<String> arrayList = myFilter(array, s -> s.split(",")[0].length() > 2,
                s -> Integer.parseInt(s.split(",")[1]) > 34);
        for (String str : arrayList) {
            System.out.println(str);
        }
    }

    private static ArrayList<String> myFilter(String[] strArray, Predicate<String> pre1, Predicate<String> pre2) {
        ArrayList<String> array = new ArrayList<>();
        for (String s : strArray) {
            if (pre1.and(pre2).test(s)) {
                array.add(s);
            }
        }
        return array;
    }
}

 

4、Function接口

package itbianma01;

import java.util.function.Function;

public class Demo4 {
    public static void main(String[] args) {
        String s="王小明,20";
        printInfo(s,a->a.split(",")[1],Integer::parseInt,i->String.valueOf(i+70));
    }
    private static void printInfo(String s, Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,String> fun3){
        String result=fun1.andThen(fun2).andThen(fun3).apply(s);
        System.out.println(result);
    }
}

 

posted @ 2023-12-21 17:02  搬砖滴  阅读(81)  评论(0编辑  收藏  举报