zoukankan      html  css  js  c++  java
  • 面试知识点总结之JDK8新特性

    HashMap在jdk7是,数组+链表 在链表状态下且是头插法 jdk8是,数组+链表+红黑树 在链表状态下是尾插法

    ----------------------------------------------------------------------------------------

    JVM内存模型,jdk7是年轻区,老年区和永久区;jdk8是年轻区,老年区和metaspace(元数据区)

    ----------------------------------------------------------------------------------------

    lambda表达式:可以实现函数式接口中的方法

    lambda表达式只能在函数式接口的支持下使用(接口只有一个抽象方法)

    语法格式:

    lambda表达式被 -> 箭头符号分成左右两部分

    左边():代表的就是函数式接口中的参数列表

    右边 {}: 代表我要实现的方法的方法体

    1.语法格式一:只有一个参数

    (String msg)->{
    System.out.println(msg);
    };

    2.语法格式二:多个参数

    (String name,int age)->{
    System.out.println("姓名:"+name+",年龄:"+age);
    };

    3.语法格式三:带有返回值

    (String name,int age)->{
    return "姓名:"+name+",年龄:"+age;
    };

    4.语法格式四:参数的类型可以省略,因为jdk可以自动类型推断

    (msg)->{
    System.out.println(msg);
    };

    5.语法格式四:如果参数只有一个可以省略()

    msg->{
    System.out.println(msg);
    };

    6.语法格式五:如果方法体中只有一条语句,{},return也是可以省略的

    //省略{}
    msg->System.out.println(msg);
    //省略return语句
    (name,age)->"姓名:"+name+",年龄:"+age;

    ----------------------------------------------------------------------------------------

    4大核心函数式接口:

    1、Consumer消费型接口

    该接口提供一个accept方法,接受一个参数,无返回值。

    @FunctionalInterface
    public interface Consumer<T> {
    /**
    * Performs this operation on the given argument.
    *
    * @param t the input argument
    */
    void accept(T t);
     我们看个例子,定义了一个lambda实现体,通过accept传入参数,打印传入的参数信息

    @Test
    public void test() {
    String str = "伴学编程";
    Consumer consumer = t -> System.out.println(t);
    consumer.accept(str);
    }
    2、Function函数型接口
    该接口提供一个apply方法,接受一个参数,有返回值。

    @FunctionalInterface
    public interface Function<T, R> {
    /**
    * Applies this function to the given argument.
    *
    * @param t the function argument
    * @return the function result
    */
    R apply(T t);
    我们看个例子,定义了一个lambda实现体,通过apply方法传入参数,打印传入的参数信息。

    @Test
    public void test() {
    String str = "伴学编程";
    Function function = t -> t;
    System.out.println(function.apply(str));
    }
    3、Supplier供给型接口
    该接口提供一个get方法,无参数,有返回值。

    @FunctionalInterface
    public interface Supplier<T> {
    /**
    * Gets a result.
    *
    * @return a result
    */
    T get();
    }
    我们看个例子,定义了一个lambda实现体,通过get方法返回并打印返回信息。 

    @Test
    public void test() {
    String str = "伴学编程";
    Supplier supplier = () -> str;
    System.out.println(supplier.get());
    }
    4、Predicate断言型接口
    该接口提供一个test方法,传入一个参数,返回boolean型值。 

    @FunctionalInterface
    public interface Predicate<T> {
    /**
    * Evaluates this predicate on the given argument.
    *
    * @param t the input argument
    * @return {@code true} if the input argument matches the predicate,
    * otherwise {@code false}
    */
    boolean test(T t);
    我们看个例子,定义了一个lambda实现体,通过test方法传入参数,返回并打印判断结果。 

    @Test
    public void test() {
    String str = "伴学编程";
    Predicate predicate = t -> "伴学编程".equals(t);
    System.out.println(predicate.test(str));

    ----------------------------------------------------------------------------------------

  • 相关阅读:
    numpy之填充为nan的数据为该列平均值
    使用Apache HttpClient 4.5设置超时时间
    使用Apache HttpClient 4.x进行异常重试
    使用Apache HttpClient 4.x发送Json数据
    用Maven创建动态Web工程
    Eclipse查看.properties文件中文乱码
    Oracle数据库常用命令(持续更新)
    Spring@PostConstruct注解和构造方法的调用顺序
    在Java中使用Maven配置的版本信息
    Maven profile动态选择配置条件
  • 原文地址:https://www.cnblogs.com/cgy-home/p/14523095.html
Copyright © 2011-2022 走看看