JAVA函数式接口

 

 

package com.cy.test;

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

/**
 * 新机测试
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("hello world");

        starThread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"线程启动");
            }
        });

        // 方法的参数是一个函数式接口   接口中只有一个方法
        starThread(()-> System.out.println(Thread.currentThread().getName()+"线程启动"));
        System.out.println("=====================================");

        ArrayList<String> list = new ArrayList<>();
        list.add("sss");
        list.add("a");
        list.add("dd");
        list.add("cccc");
        list.add("zzzzzz");

        System.out.println(list);

        Collections.sort(list);

        System.out.println(list);

        Collections.sort(list,getComparator());
        System.out.println(list);

    }

    //runable是函数式接口
    public static void starThread(Runnable run){
//        Thread thread = new Thread(run);
//        thread.start();

        new Thread(run).start();
    }


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

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

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

 

常用的函数式接口

supplier接口

​
package com.cy.test;

import java.util.function.Supplier;

/**
 * supplier接口测试
 */
public class SupplierTest {
    public static void main(String[] args) {

        String string = getString(new Supplier<String>() {
            @Override
            public String get() {
                return "hello world";
            }
        });
        System.out.println(string);

        //lambda
        String string1 = getString(() -> {
            return "hello world java";
        });
        System.out.println(string1);


        String string2 = getString(() -> "hello world java test");
        System.out.println(string2);

        Integer i = getInteger(new Supplier<Integer>() {
            @Override
            public Integer get() {
                return 9527;
            }
        });
        System.out.println(i);

        Integer i2 = getInteger(() -> 9527);
        System.out.println(i2);


        int[] arr={1,651,65,418,44,51,12,33,2};
        int max = getMax(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int max = arr[0];
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] > max) {
                        max = arr[i];
                    }
                }
                return max;
            }
        });
        System.out.println(max);

        int max1 = getMax(() -> {
            int max2 = arr[0];
            for (int j = 0; j < arr.length; j++) {
                if (arr[j] > max2) {
                    max2 = arr[j];
                }
            }
            return max2;
        });
        System.out.println(max1);
    }

    public static String getString(Supplier<String> s){
        return s.get();
    }

    public static Integer getInteger(Supplier<Integer> i){
        return i.get();
}

     public static int getMax(Supplier<Integer> i){
        return i.get();
    }
}

​

 

package com.cy.test;

import java.util.function.Predicate;

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

        boolean b = checkString("打啊三大发生大", (String s) -> {
            return s.length() > 9;
        });
        System.out.println(b);

        boolean b1 = checkString("sdfsdfsdfds", s -> s.length() > 4);
        System.out.println(b1);


        boolean b2 = check("sdfsdfsdfds", s -> s.length() > 4,s -> s.length() <16);
        System.out.println(b2);
    }
    public static boolean checkString(String s, Predicate<String> pre){
//        return pre.test(s);
//        return !pre.test(s);
        return pre.negate().test(s);


    }public static boolean check(String s, Predicate<String> pre,Predicate<String> pre2){
//        boolean b = pre.test(s);
//        boolean b2 = pre.test(s);
//        return b && b2;
        //短路与
        boolean b = pre.and(pre2).test(s);

        //短路或
        boolean b2 = pre.or(pre2).test(s);
        return b;
    }
}

 

package com.cy.test;

import java.util.function.Function;

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

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


        convert2(100,i -> String.valueOf(i+555));
    }
    public static void convert(String s, Function<String,Integer>fun){
        Integer i = fun.apply(s);
        System.out.println(i);

    }

    public static void convert2(int i, Function<Integer,String>fun){
        String s = fun.apply(i);
        System.out.println(s);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值