Changes and new features of Java 8 (not complete edition)

A change in the underlying structure HashMap

jdk1.7 HashMap: singly linked list arrays +

jdk1.8 HashMap: Array + (singly linked list / black tree)

jdk1.8 multi FIG next 3 parameters

In hashmap, when the hash collision is greater than 8 (the array subscripts a single linked list of data stored in greater than 8), and a total capacity of more than 64 (the total size of the entire hashmap greater than 64), the list structure will hashmap into a red-black tree structure store

Change reason: the query efficiency will be reduced when the list structure data is too large, while the red-black tree into a greatly increased efficiency will be in addition to other Tim accident

 

Second, the region becomes permanently delete the element space

1, PermGen (permanent generation)

"Approach area" is the JVM specification, "permanent generation" is one way to achieve district, and only the HotSpot only "PermGen space", while for other types of virtual machines is not "PermGen space".

In JDK1.8, HotSpot has no "PermGen space" this interval, replaced by a Metaspace (dimensional space)

2, Metaspace (element space)

In JDK1.8, does not exist on behalf of the permanent, class information stored compiled code data and the like has been moved to MetaSpace (element space), a meta-space and not in the heap, but directly local memory occupied ( NativeMemory).

MetaSpace permanent nature and to similar, are achieved in the method area the JVM specification.

But the biggest difference between the yuan and the permanent space on behalf of that: Yuan is not space in a virtual machine, but the use of local memory (physical memory)

3, heap memory is divided

JDK1.7 and JDK versions of its early, the heap memory is usually divided into three regions: Young Generation, Old Generation, Permanent Generation for VM Matedata

JDK1.8 stored in the metadata permanent memory into the local memory from the heap memory, JDK1.8 the JVM heap memory structure becomes as follows: 

 

Three, Lambda expressions 

Lambda表达式专门针对只有一个方法的接口(即函数式接口

Java 8 内部Lambda 表达式的实现方式在本质是以匿名内部类的形式的实现的

lambda表达式的基本格式为 (x,y...)->{表达式...};

  1. 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
  2. 可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
  3. 可选的大括号:如果主体包含了一个语句,就不需要使用大括号。
  4. 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指定明表达式返回了一个数值。

语法:

(parameters) -> expression
(parameters) -> statement
(parameters) -> { statements; }

举例:

() -> Math.PI * 2.0
(int i) -> i * 2
(String s) -> s.length()
(int i0, int i1) -> i0 + i1
(int x, int y) -> { return x + y; }

 

函数式接口

  • 只有函数式接口,才可以转换为lambda表达式,什么是函数式接口呢?
  • 有且只有一个抽象方法的接口被成为函数式接口!
  • 函数式接口可以显式的被 @FunctionalInterface 所表示,当被标识的接口不满足规定时,编译器会提示报错

Runnable接口就是一个函数式接口

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

转换方式

1、当函数式接口内的唯一抽象方法没有参数时,可以使用如下方式

//如果方法体内只有一条语句
//无返回值
() -> System.out.println("test");
//有返回值,不需要显示的return
() -> new Student();

//如果有多条语句,需要用{}包围
//无返回值
() -> {
    int a = 1;
    int b = 2;
    System.out.println(a * b);
};
//有返回值
() -> {
    int a = 1;
    int b = 2;
    return a * b;
};

2、当函数式接口内的唯一抽象方法含有参数时,写法与上面基本相同,只是需要把对应参数写到()里面

//不需要指明参数类型,会根据实际方法中的类型自动推测
//只有一个参数可以省略括号
(arg1, arg2, arg3) -> System.out.println(arg1 * arg2 * arg3);

3、举个例子

//自定义一个函数式接口
@FunctionalInterface
public interface MyInterface {
    void method(int a, int b);
}
    //使用lambda表达式
    public static void main(String[] args) {
        MyInterface myInterface = (a, b) -> System.out.println(a + b);
        myInterface.method(1, 2);//3
    }

 

发布了69 篇原创文章 · 获赞 43 · 访问量 9万+

Guess you like

Origin blog.csdn.net/fox_bert/article/details/102827695