Today I'll talk about some of the new features Java8 indeed appear Java8 new features, giving developers to bring great convenience, it may be just the beginning of such an approach would be a little uncomfortable, but when you actually after the familiar, you'll love these new features, this article on to talk about these new features.
lambda expressions
lambda expression is used in the project, and this new join syntax, the use of Java for many years, I, I think it is even more powerful feeling of Kazakhstan, this new syntax, greatly improved the previous Java code change a more concise, I think that's why Java8 can quickly pop up bar.
Here we compare with the previous several Java classical way of writing and the use of lambda expressions.
Thread usage
The original thread usage
//使用匿名内部类的方式启动多线程
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("这是使用匿名内部类的方式。。。");
}
}).start();
lambda expressions
//使用lambda表达式方式
new Thread(() -> {
System.out.println("这是使用lambda表达式的方式。。。");
}).start();
You will find that with the way the lambda expression can write less code, will look more comfortable and simple.
There is no use parameters , just a simple example.
We look at an example.
Traversal
Original way
//原始方式
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
for (int i : list) {
System.out.println(i);
}
lambda expression way
//使用lambda表达式代替foreach循环
Stream.of(1, 2, 3, 4, 5).forEach((x) -> {
System.out.println(x);
});
In the original way, we generally use foreach to traverse the way, the arrival of Java8 way, we can use forEach
the method, and then, and then the way the lambda expression to traverse, but also the original way to become more concise.
In this example, we add a parameter , in ()
the middle we added a x
meaning represented in fact: By forEach
the method, we have been assigned to an element x
in, get this x
, we can output the results.
to sum up
lambda of use is very simple and can be summed up in the following way.
([参数可选,...]) -> {
}
Method references
In fact, a simplified method references is part of a lambda expression, that is to simplify the lambda expression feeling exists, let's also talk about how to use the method reference .
/**
* @return void
* @Author ouyangsihai
* @Description 方法引用测试
* @Date 10:23 2019/5/14
* @Param []
**/
@Test
public void test_method_reference() {
//使用lambda表达式
Stream.of("A", "BB", "CCC", "DDDD", "FFFFF")
.map(s -> s.length()) //lambda
.forEach((x) -> {
System.out.println(x);
});
//使用静态方法引用
Stream.of("A", "BB", "CCC", "DDDD", "FFFFF")
.map(String::length) //静态方法引用
.forEach((x) -> {
System.out.println(x);
});
//使用实例方法引用
Stream.of(
new ClassMate("1", "欧阳思海"),
new ClassMate("2", "sihai")
).map(ClassMate::getName)//实例方法引用
.forEach(x -> {
System.out.println(x);
});
}
In the first test, we use lambda expressions to get the length of each string .
s -> s.length()
In the second test, we used a static method references to get the length of each string .
String::length
In the third test, we use the example of a reference method.
ClassMate::getName
Explanation
① map method is mapped meaning.
② forEach way through each element.
③ ClassMate po is a simple class contains the id and name.
By the above example, we basically know how to use references. Below we summarize a little.
Summary
① Use
类名::方法名
② method can be: static methods, instance methods
Constructor references
In the above, we talk about the basics of using the reference method, in fact, in addition to the reference method, as well as references to the constructor, recall, is how to create an object before we do? Is not a new subject requires it, so now with reference to the constructor is how to do it?
Let's use an example to explain what, in this case, the object or the use of the above ClassMate
.
/**
* @return void
* @Author ouyangsihai
* @Description 构造函数引用测试
* @Date 10:23 2019/5/14
* @Param []
**/
@Test
public void test_method_reference2() {
//使用lambda表达式
Stream.of("A", "BB", "CCC", "DDDD", "FFFFF")
.map(s -> new ClassMate(s)) //lambda
.collect(Collectors.toList());
//使用构造函数引用
Stream.of("A", "BB", "CCC", "DDDD", "FFFFF")
.map(ClassMate::new) //构造函数引用,由上下文决定用哪一个构造函数
.collect(Collectors.toList());
}
① first we use a lambda expression to create the object s -> new ClassMate(s)
.
② The second we use the constructor a reference to create the object ClassMate::new
.
③ We found constructor references: 类名::new
then for a constructor which is determined by the context, such as a constructor parameter and two free parameters and the parameters, which are automatically determined by a.
interface
Interface Before Java 8 is not there to achieve, can only define abstract methods, however, after Java 8, added a new feature that can be added to achieve , you can define the default method , you can define a static method .
Functional Interface
What is the function interface it?
In Java, the term is seldom heard of before, but it is with Java 8 turned out, functional programming has become familiar.
In an interface in our @FunctionalInterface
annotation declare an interface, and the interface is only an abstract method, then we called it a function interface .
/**
* @ClassName FunctionalInterfaceTest
* @Description
* @Author 欧阳思海
* @Date 2019/5/14 10:39
* @Version 1.0
**/
@FunctionalInterface
public interface FunctionalInterfaceTest {
//继承接口后,又加了新的抽象方法,这个接口就不再是函数式接口
void test(String s);
}
① above is only an abstract interface method, so this is a function interface.
② If the above interface plus an abstract method, then it is not the function interface.
Below, we'll inherit this interface through inheritance.
/**
* @ClassName FunctionalTest
* @Description
* @Author 欧阳思海
* @Date 2019/5/17 17:26
* @Version 1.0
**/
public interface FunctionalTest extends FunctionalInterfaceTest{
int test2();
}
① We inherited the above interface, and added a test2
method.
② here note that if an interface to integrate existing function interface, but also added other abstract methods, this interface is not a function of the interface.
The default method
The default method is very simple, with a default
statement can be.
/**
* @ClassName FunctionalInterfaceTest
* @Description
* @Author 欧阳思海
* @Date 2019/5/14 10:39
* @Version 1.0
**/
@FunctionalInterface
public interface FunctionalInterfaceTest {
//继承接口后,又加了新的抽象方法,这个接口就不再是函数式接口
void test(String s);
//默认方法
default String getStr(){
return null;
}
}
① add a default method in the interface. And implements the methods.
Static method
The default method is very simple, with a static
statement can be.
/**
* @ClassName FunctionalInterfaceTest
* @Description
* @Author 欧阳思海
* @Date 2019/5/14 10:39
* @Version 1.0
**/
@FunctionalInterface
public interface FunctionalInterfaceTest {
//继承接口后,又加了新的抽象方法,这个接口就不再是函数式接口
void test(String s);
//静态方法
static String getStr2(){
return null;
}
//错误用法
default static String getStr3(){
return null;
}
}
① static method implemented by static
the statement.
Note ② can not use both default and static statement.
to sum up
In this article, we talked about lambda expressions, method references, functional interfaces, static methods in an interface, use the default method of interface.