JAVA8 First Chapter

Source: Internet
Author: User
Tags getcolor stream api

Stream Processing:

1. is a series of data items that generate only one item at a time

2. The output stream of one program is probably the input stream of another program

Java8 First new feature: Functional Programming: Class Name:: Method Name---Pass as parameter

The main thrust: to use functions (methods and lambda) as an equivalent, without interaction between elements when executed.

1. Method references such as filtering all hidden files in a directory.

There is a Ishidden method inside the file class. We can take it as a function, receive a file, and return a Boolean value.

methodreferences {
  =File ("."). Listfiles (filefilter () {    
@Override
  Accept (file) { 
 file.  Ishidden (); 
  }
  });
}
methodreference{
    File ("."). listfiles (File::ishidden);    
}
Really cool Ah, class plus method can be, feel the function of FAST programming!!!!!! In fact, the function (Ishidden) is passed to the Listfiles method, and Java8 embodies a more generalized idea of the function as a value, including a lambda or an anonymous function. A program that passes a method as an equivalent, a predicate--and mathematically representing something resembling a function, He receives a parameter value and returns TRUE or FALSE.JAVA8 will pass the conditional code in as a parameter, which avoids duplicate code in the filter method. FlowExternal iterations: Do the iterative process yourself, iterate through the elements with a foreach loop, and then process the elements, internal iterations: Stream API Tip: The concept of computing clusters: Use multiple computers connected to the network to efficiently process massive amounts of data. JAVA8 offers a new style of programming, Can better use such a computer, how to better use of the temporary is unclear ..... This time to check the implementation of search engines, such as Google search, probably understand the implementation, the address: Https://www.zhihu.com/question/19937854Collection is mainly used to store and access data, Stream is primarily used for the calculation of data. Stream allows and advocates parallel processing of elements in a stream, in parallel to the stream, first the library divides the large stream into several small streams so that parallel processing, followed by the flow of this almost free parallelism, in the case of variable shared variables, You can only work if the methods passed to libraries such as filter do not interact. Behavioral parameterization (multiple implementations of interfaces)Responding to changing needs: a good principle is to abstract a similar code after writing it. Counter example: The changing needs of apple farmers define a standard interface first, for example
Policy Mode Instance
applepredicate{
    Test (Apple);
}
applepredicate{
    Test (apple) {
        Apple.   ;
    }
}
applepredicate{
    Test (apple) {
        "Green". equals (Apple.  GetColor);  
    }
}
Pass code behavior (for example, by passing different objects can invoke different implementation methods, to complete the corresponding operation)
This time, farm name if there are other needs just to create a class implementation applepredicate can be
List<Applefilterapples (list<AppleInventory,p) {   
    List<AppleArrayList<> ();   
    For (inventory) {
        if (p.Test (Apple)) {  
            Result. Add (apple); 
        }
    }
    Result
}
applepredicate{
    Test (apple) {
        "Red". equals (Apple.  Apple. getweight ()>;     
    }
}
List<Applefilterapples (inventory,appleredandheacypredicate);    
Policy mode: The behavior of a class or its algorithm is changed at run time. It belongs to the policy mode ==> forgot to look at the rookie tutorial written very clear Lambda
predicate<T>{  
    Test (t);
}
  public static <t> List< Span class= "Cm-operator" ><t>  Filter (list<T > list,Predicate<t> p) {  
    List<TArrayList<> ();   
    For (list) {
        if (PTest (e)) {  
            Result. Add (e); 
        }
    }
    Result
}
?
List<Applefilter (inventory, (apple),"Red".  Equals (Appl.getColor ()));         
?
  list<integer> evennumbers = filter (numbers, (integer i" -> i Span class= "Cm-operator" >% 2 = 0);   
Lambda Pros: Anonymous ==> no explicit name, less write think more function ==> function does not belong to a specific class, Lambda has a parameter list, function body, return type may also have thrown exception list delivery ==>lam The BDA expression can be passed as a parameter to a method or stored in a variable introduction ==> does not need to write as many template code lambda uses as anonymous classes: Functional interfaces: can be directly inline in the form of a functional interface abstract method to provide implementation, and the entire An expression as an instance of a function interface specifically: a specific implementation of a functional interface example, such as the following:<wiz_code_mirror>
Main (args) {
  = ()System.  Out. println ("Nihao");    
    Runnable () {
@Override
  Run () {
System. Out . println ("Wobuhao");   
  }
  };
R1. run (); 
R2. run (); 
}
?
Output:
Nihao
Wobuhao

JAVA8 First Chapter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.