java 8

作者: sarleon | 来源:发表于2016-05-23 01:41 被阅读0次

    1.Default method

    The default method and static method of an interface can be use a extend method in the class that implement the interface.But,an interface can't override the method extend from class Object such as equals(),hashcode();

    2.Functional interface

    if an interface has only one abstract method ("abstract" will not be declare but it is default) the interface will be describe as a functional interface eg:(Runnable,ActionListener ,etc) A functional interface could has any number of default method.

    3. Lambda

    The most valueble properties of a functional interface is that it could be turn to an instance using a lambda expresion

    (int x,int y)-> {return x+y;}

    using builder to improve code

    when you need to create a new instance of an functional interface ,you can use a lambda expression,it is often used when you need to create an annoymous inner class

    
    Runnable r1=new Runnable(){
    
        @Override
    
        public void run(){
    
            // need to do 
    
        }
    
    }
    
    Thread t1=new Thread(r1);
    
    

    using annoymous inner class:

    
    Thread t1=new Thread(new Runnable(){
    
        @Override
    
        public void run(){
    
            System.out.println("things to do here");
    
        }
    
    });
    
    

    using annoymous inner class and lambda expression

    
    Thread t1 =new Thread(()->  System.out.println("things to do here"));
    
    

    you can see that the only abstract method of the functional interface will be define in the expression,the the parameter in the bracket is the formal parameter of the abstract funtion ,

    We can create an instance of a functional interface using a lambda expression:the list of parameter is in the bracket an the abstract method is on the right of the "->" it could be an expression(a executable) or a statement(could turn to a value) as a return value

    
    (int x,int y)->{return x+y;} //
    
    x->x*x                                    //as a rturn value
    
    x->{System.out.println(x);}//as a expression
    
    String::valueOf                         x -> String.valueOf(x)
    
    Object::toString                       x -> x.toString()
    
    x::toString                                () -> x.toString()
    
    ArrayList::new                          () -> new ArrayList<>()
    
    

    4.Closure

    When you declare a local variable,that variable has a scope,outside the scope you can't use it;

    if I try to access a variable,it will find from the current scope,then the parent scope

    A closure is a function that use a free variable ,different environment will lead to different instance.

    5.eta-conversion

    Langua that support lambda often support eta-conversion,eta-conversion is building a clousure of a method

    using a Class::method to automaticly fill in the parameter;

    6.Stream api

    An object that implement Collection interface could be turn to a stream using Collection.stream() and have several method:

    sort() ,you can declare a comparator

    filter() only object that accord with the filter could be remained

    map() could deal with each object,change it

    match() if the boject match the method and return a boolean value

    count() return a value the the amount of the object in the stream.

    7.Serial and parallel

    Collection.stream() //Serial

    Collection.parallelStream() //parallel

    parallelStream use muti thread to handle the work,the runtime will partition the stream into several substream in parallel and then combine the result .

    8.laziness in parallel

    There are two kind of operation in stream ,intermediate and terminal ,an intermediate operation will be evaluate only when it required to do so(such as meet a terminal operation)

    相关文章

      网友评论

          本文标题:java 8

          本文链接:https://www.haomeiwen.com/subject/hflarttx.html