java8的官网,可以查看java8中多了什么东西
https://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
Lambda表达式
Lamdba只能用于函数式接口
public class LambdaDemo {
public static void main(String[] args) {
Cal cal=(int a,int b)->{
return a+b;
};
System.out.println(cal.add(1,2));
}
}
interface Cal{
public int add(int a, int b);
}
表达语法
public class LambdaDemo6 {
public static void main(String args[]){
LambdaDemo6 demo6 = new LambdaDemo6();
MathOperation addition = (int a, int b) -> a + b;
MathOperation subtraction = (a, b) -> a - b;
MathOperation multiplication = (int a, int b) -> { return a * b; };
MathOperation division = (int a, int b) -> a / b;
System.out.println("10 + 5 = " + demo6.operate(10, 5, addition));
System.out.println("10 - 5 = " + demo6.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + demo6.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + demo6.operate(10, 5, division));
GreetingService greetService1 = message ->
System.out.println("Hello " + message);
GreetingService greetService2 = (message) ->
System.out.println("Hello " + message);
greetService1.sayMessage("World");
greetService2.sayMessage("Java");
}
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
void sayMessage(String message);
}
private int operate(int a, int b, MathOperation mathOperation){
return mathOperation.operation(a, b);
}
}
方法引用
lambda 表达式只能引用标记了 final 的外层局部变量,这就是说不能在 lambda 内部修改定义在域外的局部变量,否则会编译错误。
public class LambdaDemo3 {
final static String message="Hello";
public static void main(String[] args) {
Hello hello=(msg)->
System.out.println(message+msg);
hello.sayHello(" World");
}
interface Hello {
void sayHello(String msg);
}
}
我们也可以直接在 lambda 表达式中访问外层的局部变量 lambda 表达式的局部变量可以不用声明为 final,但是必须不可被后面的代码修改(即隐性的具有 final 的语义)
public class LambdaDemo4 {
public static void main(String[] args) {
final String message="Hello";
Hello hello=(msg)->
System.out.println(message+msg);
hello.sayHello(" World");
}
interface Hello {
void sayHello(String msg);
}
}
构造方法的引用
创建Dog类(两个构造器)
public class Dog {
private String name;
private int age;
public Dog() {
System.out.println("无参构造方法");
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
System.out.println("name:"+name+" "+"age:"+age);
}
}
public class LambdaDemo5 {
public static void main(String[] args) {
DogService dogService=()->new Dog();
dogService.Dog();
DogService2 dogService2=Dog::new;
dogService2.Dog("旺财",9);
}
interface DogService{
Dog Dog();
}
interface DogService2{
Dog Dog(String name,int age);
}
}
@FunctionalInterface注解
函数式接口
Predicate 接口是一个函数式接口,它接受一个输入参数 T,返回一个布尔值结果。 该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与,或,非)。 该接口用于测试对象是 true 或 false。 我们可以通过以下实例来了解函数式接口 Predicate 的使用:
public class Test1 {
public static void main(String args[]){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println("输出所有数据:");
eval(list, n->true);
System.out.println("输出所有偶数:");
eval(list, n-> n%2 == 0 );
System.out.println("输出大于 3 的所有数字:");
eval(list, n-> n > 3 );
}
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n: list) {
if(predicate.test(n)) {
System.out.println(n + " ");
}
}
}
}
|