深圳做商城网站建设,wordpress登录可见内容,北京网站建设熊掌号,如何看还在建设的网站一、Java8(JDK1.8)新特性
1、Lamdba表达式
2、函数式接口
3、方法引用和构造引用
4、Stream API
5、接口中的默认方法和静态方法
6、新时间日期API
7、OPtional
8、其他特性
二、java8#xff08;JDK1.8#xff09;新特性简介
1、速度快#xff1b;
2、代码少、简…一、Java8(JDK1.8)新特性
1、Lamdba表达式
2、函数式接口
3、方法引用和构造引用
4、Stream API
5、接口中的默认方法和静态方法
6、新时间日期API
7、OPtional
8、其他特性
二、java8JDK1.8新特性简介
1、速度快
2、代码少、简介新增特性:lamdba表达式
3、强大的Stream API
4、使用并行流和串行流
5、最大化较少空指针异常Optional
其中最为核心的是Lambda表达式和Stream API
三、java8JDK1.8新特性详细介绍
一、Lambda表达式
1、Lambda表达式是什么
Lambda是一个匿名函数我们可以将Lambda表达式理解为一段可以传递的代码将代码像数据一样传递。使用它可以写出简洁、灵活的代码。作为一种更紧凑的代码风格使java语言表达能力得到提升。
2、从匿名类到Lambda转换
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Demo01 { private static Logger log LoggerFactory.getLogger(Demo01.class); public static void main(String[] args) { Runnable t1 new Runnable(){ Override public void run(){ log.info(我是没有使用Lambda表达式不简洁); } }; Runnable t2 () - log.info(我是使用Lambda表达式简洁、灵活); t1.run(); t2.run(); }}Run result
19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是没有使用Lambda表达式不简洁、代码多19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是使用Lambda表达式简洁、灵活Process finished with exit code 03、Lambda表达式语法
Lambda表达式在java语言中引入了一种新的语法元素和操作。这种操作符号为“-”,Lambda操作符或箭头操作符它将Lambda表达式分割为两部分。 左边指Lambda表达式的所有参数 右边指Lambda体即表示Lambda表达式需要执行的功能。
六种语法格式
1、语法格式一无参数、无返回值只需要一个Lambda体
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Demo02 { private static Logger log LoggerFactory.getLogger(Demo02.class); public static void main(String[] args) { Runnable t1 ()- log.info(Lambda表达式简洁、灵活优雅永不过时); t1.run(); }}run result
22:22:39.125 [main] INFO com.chen.test.JAVA8Features.Demo02 - Lambda表达式简洁、灵活优雅永不过时Process finished with exit code 02、语法格式二lambda有一个参数、无返回值
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.function.Consumer;public class Demo03 { private static Logger log LoggerFactory.getLogger(Demo03.class); public static void main(String[] args) { ConsumerString consumer new ConsumerString() { Override public void accept(String s) { log.info(s); } }; consumer.accept(爱与被爱的区别); ConsumerString consumer1 (s) - log.info(s); consumer1.accept(接受爱不一定爱对方爱一定付出真心爱); }}run result
23:03:08.992 [main] INFO com.chen.test.JAVA8Features.Demo03 - 爱与被爱的区别23:03:09.142 [main] INFO com.chen.test.JAVA8Features.Demo03 - 接受爱不一定爱对方爱一定付出真心爱Process finished with exit code 03、语法格式三Lambda只有一个参数时可以省略
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.function.Consumer;public class Demo04 { private static Logger log LoggerFactory.getLogger(Demo04.class); public static void main(String[] args) { ConsumerString consumer s - log.info(s); consumer.accept(Lambda只有一个参数时可以省略); }}
run result
23:08:27.295 [main] INFO com.chen.test.JAVA8Features.Demo04 - Lambda只有一个参数时可以省略Process finished with exit code 04、语法格式四Lambda有两个参数时并且有返回值
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Comparator;public class Demo05 { private static Logger log LoggerFactory.getLogger(Demo05.class); public static void main(String[] args) { CompareOldMethod(12,10); findMaxValue(12,10); findMinValue(12,10); }// 没有使用Lambda表达式比较大小 public static void CompareOldMethod(int num1,int num2){ ComparatorInteger comparator new ComparatorInteger() { Override public int compare(Integer o1, Integer o2) { log.info(o1:{},o1); log.info(o2:{},o2); return o1 o2 ? o2 : o1; } }; log.info(OldFindMaxValue:{},comparator.compare(num1,num2)); }// 使用lambda表达式 public static void findMaxValue(int num1,int num2){ ComparatorInteger comparatorMax (o1, o2) -{ log.info(o1:{},o1); log.info(o2:{},o2); return (o1o2)? o2 :(o1); }; log.info(findMaxValue:{},(comparatorMax.compare(num1,num2))); } public static void findMinValue(int num1,int num2){ ComparatorInteger comparatorMin (o1, o2) - { log.info(o1:{},o1); log.info(o2:{},o2); return (o1 o2) ? o1 : o2; }; log.info(FindMinValue:{},comparatorMin.compare(num1,num2)); }}run result
00:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:1200:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:1000:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - OldFindMaxValue:1200:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:1200:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:1000:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - findMaxValue:1200:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:1200:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:1000:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - FindMinValue:10Process finished with exit code 05、语法格式五当Lambda体只有一条语句的时候return和{}可以省略掉
package com.chen.test.JAVA8Features;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Comparator;public class Demo05 { private static Logger log LoggerFactory.getLogger(Demo05.class); public static void main(String[] args) { findMaxValue(12,10); findMinValue(12,10); }// 使用lambda表达式 public static void findMaxValue(int num1,int num2){ ComparatorInteger comparatorMax (o1, o2) -{ log.info(o1:{},o1); log.info(o2:{},o2); return (o1o2)? o2 :(o1); }; log.info(findMaxValue:{},(comparatorMax.compare(num1,num2))); } public static void findMinValue(int num1,int num2){ ComparatorInteger comparatorMin (o1, o2) - (o1 o2) ? o1 : o2; log.info(FindMinValue:{},comparatorMin.compare(num1,num2)); }}run result
00:22:31.059 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:1200:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:1000:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - findMaxValue:1200:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - FindMinValue:10Process finished with exit code 06、语法格式六类型推断数据类型可以省略因为编译器可以推断得出成为“类型推断”
package com.chen.test.JAVA8Features;import com.mysql.cj.callback.MysqlCallbackHandler;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.function.Consumer;public class Demo07 { private static Logger log LoggerFactory.getLogger(Demo07.class); public static void main(String[] args) { dateType(); } public static void dateType(){ ConsumerString consumer (String s) - log.info(s); consumer.accept(Hello World !); ConsumerString consumer1 (s) - log.info(s); consumer1.accept(Hello dont date type !); }}
二、函数式接口
1、什么是函数式接口
函数式接口只包含一个抽象方法的接口称为函数式接口并且可以使用lambda表达式来创建该接口的对象可以在任意函数式接口上使用FunctionalInterface注解来检测它是否是符合函数式接口。同时javac也会包含一条声明说明这个接口是否符合函数式接口。
2、自定义函数式接口
package com.chen.test.JAVA8Features;FunctionalInterfacepublic interface FunctionDemo1 { public void fun();}
3、泛型函数式接口
package com.chen.test.JAVA8Features;FunctionalInterfacepublic interface FunctionGenericT { public void fun(T t);}
4、java内置函数式接口
(Function、Consumer、Supplier、Predicate) java.util.function
Function (函数型接口)
函数型接口有输入参数也有返回值。 * param T the type of the input to the function * param R the type of the result of the function * * since 1.8 */FunctionalInterfacepublic interface FunctionT, R { /** * Applies this function to the given argument. * * param t the function argument * return the function result */ R apply(T t);其中T表示输入参数R为返回值
代码展示 public void functionTest(){// Function function new FunctionString,String(){// Override// public String apply(String s) {// return s;// }// };// log.info(函数型接口 :{},function.apply(没有使用Lambda表达式)); Function function s - s; log.info(函数型接口{},function.apply(Function Demo)); }Consumer消费型接口
消费型接口有入参没有会有返回值
* param T the type of the input to the operation * * since 1.8 */FunctionalInterfacepublic interface ConsumerT { /** * Performs this operation on the given argument. * * param t the input argument */ void accept(T t);代码展示 public void consumerTest(){// 非Lambda表达式// ConsumerString consumer new ConsumerString() {// Override// public void accept(String s) {// log.info(s);// }// };// consumer.accept(消费型函数没有使用Lambda表达式);// 使用Lambda表达式 ConsumerString consumer s - log.info(s); consumer.accept(消费型函数Consumer Demo); }
Supplier供给型接口
供给型接口没有输入参数有返回值 * * param T the type of results supplied by this supplier * * since 1.8 */FunctionalInterfacepublic interface SupplierT { /** * Gets a result. * * return a result */ T get();}代码展示 public void supplierTest(){// 非Lambda表达式// Supplier supplier new SupplierString(){// Override// public String get() {// return 供给型接口没有使用Lambda表达式;// }// };// log.info(String.valueOf(supplier.get())); Supplier supplier () - 供给型接口Supplier Demo; log.info(String.valueOf(supplier.get())); }Predicate断定型接口
断言型接口既有输入参数也有返回值返回类型是boolean类型
* param T the type of the input to the predicate * * since 1.8 */FunctionalInterfacepublic interface PredicateT { /** * 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);展示代码
public void predicateTest() {// PredicateString predicate new PredicateString() {// Override// public boolean test(String s) {// return s.equals(Predicate Demo);// }// };// log.info(断言型接口{},predicate.test(没有使用Lambda表达式)); PredicateString predicate s - s.equals(Predicate Demo); log.info(断言型接口{},predicate.test(Predicate Demo)); }java内置四种大函数式接口可以使用Lambda表达式
package com.chen.test.JAVA8Features;import com.google.common.base.Function;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.function.Consumer;import java.util.function.Predicate;import java.util.function.Supplier;public class FunDemo01 { private static Logger log LoggerFactory.getLogger(FunDemo01.class); public static void main(String[] args) { FunDemo01 demo01 new FunDemo01(); demo01.functionTest(); demo01.consumerTest(); demo01.supplierTest(); demo01.predicateTest(); } public void functionTest(){// 非Lambda表达式// Function function new FunctionString,String(){// Override// public String apply(String s) {// return s;// }// };// log.info(函数型接口 :{},function.apply(没有使用Lambda表达式)); Function function s - s; log.info(函数型接口{},function.apply(Function Demo)); } public void consumerTest(){// 非Lambda表达式// ConsumerString consumer new ConsumerString() {// Override// public void accept(String s) {// log.info(s);// }// };// consumer.accept(消费型函数没有使用Lambda表达式);// 使用Lambda表达式 ConsumerString consumer s - log.info(s); consumer.accept(消费型函数Consumer Demo); } public void supplierTest(){// 非Lambda表达式// Supplier supplier new SupplierString(){// Override// public String get() {// return 供给型接口没有使用Lambda表达式;// }// };// log.info(String.valueOf(supplier.get())); Supplier supplier () - 供给型接口Supplier Demo; log.info(String.valueOf(supplier.get())); } public void predicateTest() {// PredicateString predicate new PredicateString() {// Override// public boolean test(String s) {// return s.equals(Predicate Demo);// }// };// log.info(断言型接口{},predicate.test(没有使用Lambda表达式)); PredicateString predicate s - s.equals(Predicate Demo); log.info(断言型接口{},predicate.test(Predicate Demo)); }}
三、方法引用和构造器引用
1、方法引用
当要传递给Lambda体的操作已经有实现方法可以直接使用方法引用(实现抽象方法的列表必须要和方法引用的方法参数列表一致)
方法引用使用操作符“”将方法名和类或者对象分割开来。
有下列三种情况 对象实例方法 类实例方法 类静态方法
代码展示
package com.chen.test.JAVA8Features;public class MethodRefDemo { public static void main(String[] args) { FunctionGenericString strName s - System.out.println(s); strName.fun(Lambda表达式没有使用方法引用); //方法引用 FunctionGenericString strName2 System.out::println; strName2.fun(使用方法引用); }}2、构造器引用
本质上构造器引用和方法引用相识只是使用了一个new方法
使用说明函数式接口参数列表和构造器参数列表要一致该接口返回值类型也是构造器返回值类型
格式ClassName :: new
代码展示
package com.chen.test.JAVA8Features;import java.util.function.Function;public class MethodRefDemo { public static void main(String[] args) { //构造器引用 FunctionString, Integer fun1 (num) - new Integer(num); FunctionString, Integer fun2 Integer::new; //数组引用 FunctionInteger,Integer[] fun3 (num) -new Integer[num]; FunctionInteger,Integer[] fun4 Integer[]::new; }}四、强大的Stream API
1、什么是Stream
Java8中两个最为重要特性第一个的是Lambda表达式另一个是Stream API。
StreamAPI它位于java.util.stream包中StreamAPI帮助我们更好地对数据进行集合操作它本质就是对数据的操作进行流水线式处理也可以理解为一个更加高级的迭代器,主要作用是遍历其中每一个元素。简而言之,StreamAP提供了一种高效且易于使用的处理数据方式。
2、Stream特点 1、Stream自己不会存储数据。 2、Stream不会改变源对象。相反它们会返回一个持有结果的新Stream对象 3、Stream操作时延迟执行的。这就意味着它们等到有结果时候才会执行。 和list不同Stream代表的是任意Java对象的序列且stream输出的元素可能并没有预先存储在内存中而是实时计算出来的。它可以“存储”有限个或无限个元素。 例如我们想表示一个全体自然数的集合使用list是不可能写出来的因为自然数是无线的不管内存多大也没法放到list中但是使用Sream就可以
3、Stream操作的三个步骤 1、创建Stream一个数据源例如set 、list获取一个流 2、中间操作一个中间操作连接对数据源的数据进行处理 3、终止操作:一个终止操作执行中间操作连产生结果。
1、创建流
创建流方式有多种
第一种通过集合
对于Collection接口List 、Set、Queue等直接调用Stream()方法可以获取Stream ListString list new ArrayList(); StreamString stringStream list.stream(); //返回一个顺序流 StreamString parallelStream list.parallelStream(); //返回一个并行流可多线程第二种通过数组
把数组变成Stream使用Arrays.stream()方法 StreamString stream1 Arrays.stream(new String[]{CBB, YJJ, CB, CJJ});第三种Stream.of()静态方法直接手动生成一个Stream StreamString stream Stream.of(A, B, C, D);第四种创建无限流 //迭代 //遍历10个奇数 Stream.iterate(1,t-t2).limit(10).forEach(System.out::println); //生成 //生成10个随机数 Stream.generate(Math::random).limit(10).forEach(System.out::println);第五种自己构建
第六种其他等等
2、中间操作
一个流可以后面跟随着0个或者多个中间操作其目的是打开流做出某种程度的数据过滤、去重、排序、映射、跳过等。然后返回一个新的流交给下一个使用仅仅是调用这个方法没有真正开始遍历。
map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
3、终止操作一个终止操作执行中间操作连产生结果。
forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
package com.chen.test.JAVA8Features.Stream;import java.util.*;import java.util.stream.Collectors;public class StreamDemo01 { public static void main(String[] args) { ListInteger list new ArrayList(); for (int i 0; i 10; i) { list.add(i); } //map ListInteger collect list.stream().map(n - n * 2).collect(Collectors.toCollection(ArrayList::new)); collect.forEach(System.out::println); //filer 过滤 ListInteger list1 list.stream().filter(n - n % 2 0).collect(Collectors.toList()); list1.forEach(System.out::println); //distinct 去重 ListInteger list2 list.stream().distinct().collect(Collectors.toList()); list2.forEach(System.out::println); //skip 跳过 ListInteger list3 list.stream().skip(3).collect(Collectors.toList()); list3.forEach(System.out::println); //limit 截取 SetInteger set list.stream().limit(3).collect(Collectors.toSet()); set.forEach(System.out::println); //skip and limit 组合使用 ListInteger list4 list.stream().skip(3).limit(5).collect(Collectors.toList()); list4.forEach(System.out::println); }}五、接口中默认方法和静态方法
1、默认方法
java8允许接口中包含具体实现的方法体该方法是默认方法它需要使用default关键字修饰
2、静态方法
java8中允许接口中定义静态方法使用static关键字修饰
代码展示
package com.chen.test.JAVA8Features.DefaultMethod;public interface DefaultMethodDemo { default Integer addMethod(int a ,int b){ System.out.println(我是默认方法); return ab; } static void test(){ System.out.println(我是静态方法); }}六、新时间日期接口
七、Optional类
optional类是一个容器代表一个值存在或者不存在原来使用null表示一个值存不存在现在使用optional可以更好的表达这个概念并且可以避免空指针异常。
Optional常用的方法 Optional.of(T t) : 创建一个Optional实例 Optional.empty() : 创建一个空的Optional实例 Optional.ofNullable(T t) :若t不为空创建一个Optional实例否则创建一个空实例 isPresent() : 判断是否包含值 orElse(T t) 如果调用对象包含值返回该值否则返回t orElseGet(Supplier s) : 如果调用对象包含值返回该值否则返回s获取的值 mapFunction f 如果有值对其处理并返回处理后的Optional否则返回Optional.empty(); flatMap(Function mapper) : 与map类似要求返回值必须是Optional。