zoukankan      html  css  js  c++  java
  • Java8 新特性

    Java8新特性相关学习

    一、内建函数式接口

    1. 功能型函数式接口

    1.1 接口定义    (有参数,有返回值)

    @FunctionalInterface
    public interface Function<T, R> {
        R apply(T t);
    
        //...
    }

    1.2 接口使用

    import java.util.function.Function;
    
    public class MyTempTest {
    
        // 功能性函数式接口
        @Test
        public void test01(){
            //补充: 实例方法的引用   实例::方法
            Function<String,Boolean> fun = "**Hello" :: startsWith;
            System.out.println(fun.apply("**"));
        }
    
    }

    2.  消费型函数式接口

    1.1 接口定义  (有参数,没有返回值)

          只能进行数据处理的操作,而没有任何返回;

    @FunctionalInterface
    public interface Consumer<T> {
    
        void accept(T t);
    
    }

    2.1 接口使用

    import java.util.function.Consumer;
    
    public class MyTempTest {
    
        // 消费型函数式接口
        @Test
        public void test02(){
            Consumer<String> con = System.out :: println;
            con.accept("hello world");
        }
    
    }

    3.  供给型函数式接口

    3.1 接口定义  (没有参数,有返回值)

    例如,在String类中提供有转小写方法,这个方法没有接收参数,但是有返回值。public String toLowerCase();

    @FunctionalInterface
    public interface Supplier<T> {
    
        T get();
    }

    3.1 接口使用

    import java.util.function.Supplier;
    
    public class MyTempTest {
    
        // 供给型函数式接口
        @Test
        public void test03(){
            Supplier<String> sup = "ABC" :: toLowerCase;
            System.out.println(sup.get());
        }
    }

    4.  断言型函数式接口

    4.1 接口定义  (进行判断处理)

        例如:String类中有一个equalsIgnoreCase() 方法

    @FunctionalInterface
    public interface Predicate<T> {
    
        boolean test(T t);
    }

    4.2 接口使用

    import java.util.function.Predicate;
    
    /**
     * @author: Linwei
     * @date 2021/5/29
     * @Description:
     */
    
    public class MyTempTest {
    
        // 断言型函数式接口
        @Test
        public void test04(){
            Predicate<String> pre = "HELLO" :: equalsIgnoreCase;
            System.out.println(pre.test("hello"));
        }
    }
    边系鞋带边思考人生.
  • 相关阅读:
    几个论坛上看到的2015小米笔试题
    Line(扩展欧几里得)
    MapReduce编程之倒排索引
    annotation(@Retention@Target)详解
    【JEECG技术文档】JEECG平台对外接口JWT应用文档V3.7.2
    jeecg 模糊查询
    jeecg下实现自动默认模糊查询
    The packaging for this project did not assign a file to the build artifact
    Maven添加本地Jar包
    maven 如何引入本地jar包
  • 原文地址:https://www.cnblogs.com/crazytrip/p/14825237.html
Copyright © 2011-2022 走看看