[Java基础]函数式接口

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java基础]函数式接口相关的知识,希望对你有一定的参考价值。

在这里插入图片描述

代码如下:

package MyInterfacePack01;

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

package MyInterfacePack01;

public class MyInterfaceDemo {
    public static void main(String[] args)
    {
        MyInterface my= ()->System.out.println("hello world");
        my.show();
    }

}

在这里插入图片描述

代码如下:

package MyInterfacePack01;

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).start();
    }

}

在这里插入图片描述

代码如下:

package MyInterfacePack01;

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

public class ComparatorDemo {
    public static void main(String[] args)
    {
        ArrayList<String> array = new ArrayList<String>();

        array.add("hello");
        array.add("javaaaaa");
        array.add("world");

        System.out.println("排序前"+array);

        Collections.sort(array,getComparator());

        System.out.println("排序后"+array);


    }

    private static Comparator<String> getComparator(){
//        Comparator<String> comp = new Comparator<String>() {
//            @Override
//            public int compare(String s1, String s2) {
//                return s1.length()-s2.length();
//            }
//        };
//        return comp;

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

在这里插入图片描述
在这里插入图片描述

代码如下:

package SupplierPack;

import java.util.function.Supplier;

public class SupplierDemo {
    public static void main(String[] args)
    {
        String s = getString(()->{
            return "Tom";
        });

        String s1 = getString(()->"TOM");


        System.out.println(s);

        System.out.println(s1);

        Integer i = getInteger(()->30);
        System.out.println(i);

    }
    private static String getString(Supplier<String> sup)
    {
        return sup.get();
    }

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


}

在这里插入图片描述

代码如下:

package SupplierPack;


import java.util.function.Supplier;

public class SupplierTest {
    public static void main(String[] args)
    {
        int [] arr = {19,50,28,37,64};
       int maxValue =  getMax(()->{
            int max = arr[0];
            for (int i = 1;i<arr.length;i++)
            {
                if (arr[i] > max)
                {
                    max = arr[i];
                }
            }
            return max;
        });

       System.out.println(maxValue);
    }

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

}

在这里插入图片描述

代码如下:

package SupplierPack;

import java.util.function.Consumer;

public class ConsumerDemo {
    public static void main(String[] args)
    {
//        operatorString("Tom",(String s)->{
//            System.out.println(s);
//        });

        operatorString("Tom",s->System.out.println(s));
        operatorString("Tom",System.out::println);

        operatorString("Tom",s->
            System.out.println(new StringBuilder(s).reverse().toString())
        );

        System.out.println("------------------------------------------");

        operatorString("Jack",s->System.out.println(s),s->System.out.println(new StringBuilder(s).reverse().toString()));

    }

    private static void operatorString(String name,Consumer<String> con1,Consumer<String> con2)
    {
//        con1.accept(name);
//        con2.accept(name);
        con1.andThen(con2).accept(name);
    }


    private static void operatorString(String name, Consumer<String> con)
    {
        con.accept(name);
    }

}

在这里插入图片描述

代码如下:

package SupplierPack;

import java.util.function.Consumer;

public class ConsumerTest {
    public static void main(String[] args)
    {

        String[] strArray = {"Tom,20","Jack,23","Bom,18"};

        printInfo(strArray,(String str)->{
            String name = str.split(",")[0];
            System.out.print("姓名"+name);

        },(String str)->{
            int age = Integer.parseInt(str.split(",")[1]);
            System.out.println(",年龄"+age);
        });

    }

    public static void printInfo(String[] strArray, Consumer<String> con1,Consumer<String> con2)
    {
        for (String str:strArray)
        {
            con1.andThen(con2).accept(str);
        }
    }

}

在这里插入图片描述

代码如下:

package SupplierPack;

import java.util.function.Predicate;

public class PredicateDemo01 {
    public static void main(String[] args)
    {
        boolean b1 = checkString("hello",(String s)->{
            return s.length() > 8;
        });

        boolean b2 = checkString("hello",s->s.length() > 8);
        boolean b3 = checkString("helloworld",s->s.length() > 8);

        System.out.println(b1);

        System.out.println(b2);

        System.out.println(b3);

    }

    private static boolean checkString(String s, Predicate<String> pre)
    {
//        return pre.test(s);
        return pre.negate().test(s);
    }

}

package PredicatePack01;

import java.io.StringReader;
import java.util.function.Predicate;

public class PredicateDemo01 {
    public static void main(String[] args)
    {
        boolean b1 = checkString("hello",s->s.length() > 8);
        System.out.println(b1);
        boolean b2 = checkString("helloworld",s->s.length()> 8);
        System.out.println(b2);

        boolean b3 = checkString("hello",s->s.length() > 8 ,s->s.length()<15);
        System.out.println(b3);

        boolean b4 = checkString("helloworld",s->s.length() > 8 , s->s.length() < 15);
        System.out.println(b4);

    }

    private static boolean checkString(String s, Predicate<String> pre1,Predicate<String> pre2)
    {
//        boolean b1 = pre1.test(s);
//        boolean b2 = pre2.test(s);
//        boolean b = b1 && b2;
//        return b;
//        return pre1.and(pre2).test(s);
        return pre1.or(pre2).test(s);
    }


    private static boolean checkString(String s, Predicate<String> pre)
    {
        return pre.test(s);
    }

}

在这里插入图片描述

代码如下:

package PredicatePack01;

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

public class PredicatePack {
    public static void main(String[] args)
    {
        String[] strArray = {"Tom,30","Jack,34","Bom,49","Lily,34","Mike,33"};

        ArrayList<String> array = myFilter(strArray,s->s.split(",")[0].length()>2,s->Integer.parseInt(s.split(",")[1]) > 33);

        for (String str:array)
        {
            System.out.println(str);
        }

    }

    private static ArrayList<String> myFilter(String[] strArray, Predicate<String> pre1,Predicate<String> pre2)
    {
        ArrayList<String> array = new ArrayList<String>();

        for (String str:strArray)
        {
            if(pre1.and(pre2).test(str))
                array.add(str);
        }

        return array;
    }
}

在这里插入图片描述

代码如下:

package PredicatePack01;

import java.util.function.Function;

public class FunctionDemo {
    public static void main(String[] args)
    {
//        convert("100",(String s)->{
//            return Integer.parseInt(s);
//        });

        convert("100",s->Integer.parseInt(s));
        convert("100",Integer::parseInt);

        convert(100,i->String.valueOf(i+566));


        convert("100",s->Integer.parseInt(s),i->String.valueOf(i+566));

    }

    private static void convert(String s,Function<String,Integer> fun1,Function<Integer,String > fun2)
    {
Java 8 新特性总结

Java基础49:两个函数式接口

Java基础36:函数式编程

Java基础:封装

Java基础—— 匿名类函数式接口

精练代码:一次Java函数式编程的重构之旅