Java8 three notes

10, Optional category

Optional <T> class (java.util.Optional) is a container class, a value representative of the presence or absence of the original represented by a null value does not exist, it may be better expressed Optional concept. And avoid a null pointer exception.
⑴, commonly used methods:
Optional.of (T t): Create an instance of Optional
Optional.empty (): Creates an empty Optional instance
Optional.ofNullable (T t): If t is not null, create Optional instance, or create an empty examples
isPresent (): determining whether contains the value
orElse (T t): If the calling object contains a value, this value is returned, otherwise T
orElseGet (Supplier s): if the value of the invoking object contains a value, this value is returned, otherwise s acquired
map (Function f): If there is a value to its processing and returns the Optional treated, otherwise Optional.empty ()
flatMap (Function Mapper): similar to the map, the return value must be requested Optional

/ ** 
 * a, Optional containers: Avoid a NullPointerException 
 * Optional.of (T t): Create an instance Optional 
 * Optional.empty (): Create an empty instance Optional 
 * Optional.ofNullable (T t ): If t is not created Optional instance otherwise created empty instance as null,, 
 * isPresent (): determining whether contains the value 
 * orElse (T t): If the calling object contains a value, this value is returned, otherwise t 
 * orElseGet (Supplier S ): If you call the object contains the value, return that value, otherwise it returns the value s obtained 
 * map (Function f): If there is a value to its processing and returns the Optional treated, otherwise Optional.empty () 
 * flatMap (Function mapper): similar to the map, the return value must be required Optional the 
 * / 
public  class TestOptional { 
   
   @Test 
   public  void Test3 () { 
      Optional the <the Employee> OP = Optional.of ( new new Employee(101, "张三", 18, 9999.99));
      
      Optional<String> op2 = op.map(Employee::getName);
      System.out.println(op2.get());
      
      Optional<String> op3 = op.flatMap((e) -> Optional.of(e.getName()));
      System.out.println(op3.get());
   }
   
   @Test
   public void test2(){
      Optional<Employee> op = Optional.ofNullable(new Employee());
      //判断是否有值
      if(op.isPresent()){
         System.out.println(op.get());
      }
      
      Employee emp = op.orElse(new Employee(100,"张三",19,888.88));
      System.out.println(emp +"=====");
      
      Employee emp2 = op.orElseGet(() -> new Employee());
      System.out.println(emp2);
   }

   @Test
   public void test1(){
      Optional<Employee> op = Optional.of(new Employee());
      Employee emp = op.get();
      System.out.println(emp);
   }
}

11, the default interface methods, and static methods

⑴, the default interface methods
in Java 8 allows the interface contains a specific implementation of the method, the method called the "default method", the default method uses the default keyword modification.

interface MyFunc<T>{
    T func(int a);
    
    default String getName(){
        return "Hello Java8!";
    }
}

"Priority class" principle ⑵, default method of the interface
if an interface defines a default method, while the other parent class or interface has a defined method of the same name when a
method of selecting the parent class ①. If a parent provides a specific implementation, the interface has the same name as the default method and parameters are ignored.
② Interface conflict. If a parent interface provides a default method, while the other interface is also provided a method with the same name and argument list (regardless of whether the method is the default method), it must override this method to resolve the conflict.

interface MyFunc{
    default String getName(){
        return "Hello Java8!";
    }
}

interface Named{
    default String getName(){
        return "Hello world!";
    }
}

class MyClass implements MyFunc, Named{
    @Override
    public String getName(){
        return Named.super.getName();
    }
}

⑶, in Java8, the interface allows you to add a static method.

interface Named{
    public Integer myFun();
    
    default String getName(){
        return "Hello world!";
    }
    
    static void show(){
        System.out.println("Hello Lambda!");
    }
}

//实现类
public class Test implements Named {
    @Override
    public Integer myFun() {
        return null;
    }

    @Override
    public String getName() {
        return Named.super.getName();
    }

    public static void main(String[] args) {
        Named.show();
    }
}

12, repeated notes and type annotations

Java 8 pair of annotations treatment provides two improvements: repeatable annotations and may be used for types of notes.

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations{
    MyAnnotation[] value();
}

@Repeatable(MyAnnotations.class)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ElementType.TYPE_PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
    String value();
}

@MyAnnotation("Hello")
@MyAnnotation("World")
public void show(@MyAnnotation("abc") String str){}

 

Guess you like

Origin www.cnblogs.com/weilz/p/11334709.html