> 文章列表 > 【jdk1.8的新特性】

【jdk1.8的新特性】

【jdk1.8的新特性】

学习内容:

  1. lambda表达式
  2. 函数式接口
  3. 方法引用
  4. stream流式编程

学习产出:

lambda表达式

Lambda适用于只有一个抽象方法的接口

第一种无参数无返回值

public interface MyInterfaceA {//接口中的方法默认是抽象方法,public abstract修饰void test();
}//无参数无返回值//简化写法,只有一行代码可以使用简化写法MyInterfaceA a=()-> System.out.println("aaa");a.test();//常规方法a=()->{System.out.println("bbb");System.out.println("ccc");};a.test();

单参无返回值

public interface MyInterfaceA {//接口中的方法默认是抽象方法,public abstract修饰void test(String userName);
}MyInterfaceA myInterfaceA=(String a)->{System.out.println(a+a);};myInterfaceA.test("aaa");//类型可以省略MyInterfaceA myInterfaceB=(a)->{System.out.println(a+a);};//参数的括号可以省略MyInterfaceA myInterfaceC=a->{System.out.println(a+a);};

多参无返回值

public interface MyInterfaceA {//接口中的方法默认是抽象方法,public abstract修饰void test(String userName,String passWord);
}//多参数,参数括号不能省略MyInterfaceA myInterfaceA=(a,b)->{System.out.println(a+b);};

无参有返回值

public interface MyInterfaceA {//接口中的方法默认是抽象方法,public abstract修饰String test();
}MyInterfaceA myInterfaceA=()->{System.out.println("aaa");return "aaa" ;};//简化写法,只有一行代码的时候,可以省略return和花括号MyInterfaceA myInterfaceB=()-> "aaa";

剩下的可以依据上面的类推


函数式接口

函数式接口:接口中只能有一个抽象方法,其他的可以有default,static,Object里面继承的方法
作用:在Java中主要用在Lambda表达式和方法引用(想要使用Lambda表达式,接口必须是函数式接口)
jdk8提供了@FunctionalInterface,来对函数式接口进行检查

内置的函数式接口

消费型接口:void accept(T t) 有参数(单参)无返回值

 ArrayList<Integer> list=new ArrayList<>();list.add(1);list.forEach(new Consumer<Integer>() {@Overridepublic void accept(Integer integer) {System.out.println(integer);}});list.forEach((Integer integer)->{System.out.println(integer*10);});list.forEach(x -> System.out.println(x*10));

供给型接口:T get() 无参数有返回值

Supplier<String> supplier=new Supplier<String>() {@Overridepublic String get() {return people.getName();}};

函数型接口:R apply(T t) 有
断言型接口:

//Predicate<Integer> predicate=a->{if (a<10)return false;return true;};//为true的会删掉boolean b = list.removeIf(a->{if (a<10)return false;return true;});

方法引用

方法引用符号 ::

public class People {String name;String age;//get,set方法省略}Consumer<Integer> consumer=x-> System.out.println(x);Consumer<Integer> consumer1=System.out::println;consumer.accept(1);consumer.accept(2);list.forEach(System.out::println);Supplier<String> supplier1=people::getName;Comparator<Integer> comparator=(x,y)->Integer.compare(x,y);
Comparator<Integer> comparator1=Integer::compare;

stream流式编程

给程序员提供了一堆处理数据的方法
【jdk1.8的新特性】

//这里要注意的是coun(),foreach都属于结束操作,具体的建议百度ArrayList<Integer> list = new ArrayList<>();Collections.addAll(list,1,3,45,76,3,2,354,435,6);Stream<Integer> stream = list.stream();Stream<Integer> stream1 = stream.filter(x -> {return x > 45;}).distinct().sorted(Integer::compare).limit(3);//stream1.forEach(x-> System.out.println(x));stream1.forEach(System.out::println);

上面的多个中间操作连在一起,叫做串行流
还有一个并行流


Optional

主要目的是为了避免空指针异常的发生

        //如果传入的参数是空,会抛空指针异常//Optional<People> people1 = Optional.of(people);//传递的参数为空,创建Optional对象Optional<People> people2 = Optional.ofNullable(people);//如果对象不为空,orElse还是会创建对象People people1 = Optional.ofNullable(people).orElse(new People());//如果对象不为空,ofNullable不会创建对象People people2 = Optional.ofNullable(people).orElseGet(()->new People());