classList<E>{static<Z>List<Z>nil(){...};static<Z>List<Z>cons(Zhead,List<Z>tail){...};Ehead(){...}}//通过方法赋值的目标参数来自动推断泛型的类型List<String>l=List.nil();//而不是显示的指定类型//List<String> l = List.<String>nil();//通过前面方法参数类型推断泛型的类型List.cons(42,List.nil());//而不是显示的指定类型//List.cons(42, List.<Integer>nil());
publicinterfaceA{defaultvoidfoo(){System.out.println("Calling A.foo()");}}publicinterfaceB{defaultvoidfoo(){System.out.println("Calling B.foo()");}}publicclassABimplementsA,B{}//Duplicate default methods named foo with the parameters () and () are inherited from the types B and A。
publicclassResource{publicResource(){System.out.println("Opening resource");}publicvoidoperate(){System.out.println("Operating on resource");}publicvoiddispose(){System.out.println("Disposing resource");}}//调用Resourceresource=newResource();try{resource.operate();}finally{resource.dispose();}
但是有一个问题,如果很多地方都要用到这个资源,那么就存在很多段类似这样的代码,这很明显违反了DRY(Don’t Repeat It Yourself)原则。而且如果某位程序员由于某些原因忘了用try/finally处理资源,那么很可能导致内存泄漏。那咋办呢?Java 8提供了一个Consumer接口,代码改写为如下:
因为对资源对象resource执行operate方法时可能抛出RuntimeException,所以需要在finally语句块中释放资源,防止可能的内存泄漏。
publicclassResource{publicResource(){System.out.println("Opening resource");}publicvoidoperate(){System.out.println("Operating on resource");}publicvoiddispose(){System.out.println("Disposing resource");}publicstaticvoidwithResource(Consumer<Resource>consumer){Resourceresource=newResource();try{consumer.accept(resource);}finally{resource.dispose();}}publicstaticvoidmain(String[]args){Resource.withResource(resource->resource.operate());}}Console:OpeningresourceOperatingonresourceDisposingresource
Optionalempty=Optional.ofNullable(null);//执行下面的代码会输出:No value present try{//在空的Optional实例上调用get(),抛出NoSuchElementExceptionSystem.out.println(empty.get());}catch(NoSuchElementExceptionex){System.out.println(ex.getMessage());}
ifPresent:如果Optional实例有值则为其调用Consumer,否则不做处理
12345
Optional<String>name=Optional.of("Java");name.ifPresent((value)->{System.out.println("The length of the value is: "+value.length());//The length of the value is: 4});
orElse:如果有值则将其返回,否则返回指定的其它值。
12345678
Optional<Object>empty=Optional.ofNullable(null);Optional<String>name=Optional.of("not null");System.out.println(empty.orElse("There is no value present!"));System.out.println(name.orElse("There is some value!"));Console:Thereisnovaluepresent!notnull
try{//orElseThrow与orElse方法类似。与返回默认值不同,//orElseThrow会抛出lambda表达式或方法生成的异常 empty.orElseThrow(ValueAbsentException::new);}catch(Throwableex){//输出: No value present in the Optional instanceSystem.out.println(ex.getMessage());}classValueAbsentExceptionextendsThrowable{publicValueAbsentException(){super();}publicValueAbsentException(Stringmsg){super(msg);}@OverridepublicStringgetMessage(){return"No value present in the Optional instance";}}
Optional<String>name=Optional.of("not null");Optional<String>upperName=name.map((value)->value.toUpperCase());//输出:NOT NULLSystem.out.println(upperName.orElse("No value found"));
Optional<String>name=Optional.of("not null");name=name.flatMap((value)->Optional.of(value.toUpperCase()));//输出:NOT NULLSystem.out.println(name.orElse("No value found"));
Optional<String>name=Optional.of("Sanaulla");//filter方法检查给定的Option值是否满足某些条件。//如果满足则返回同一个Option实例,否则返回空Optional。Optional<String>longName=name.filter((value)->value.length()>6);System.out.println(longName.orElse("The name is less than 6 characters"));//输出Sanaulla//另一个例子是Optional值不满足filter指定的条件。Optional<String>anotherName=Optional.of("Sana");Optional<String>shortName=anotherName.filter((value)->value.length()>6);//输出:The name is less than 6 charactersSystem.out.println(shortName.orElse("The name is less than 6 characters"));