Java8 the fourth Chapter

Source: Internet
Author: User

Stream operations can be executed sequentially or in parallel to why.???

Flow

--java API new member ==> traversal data set advanced iterator

--Features: transparent parallel processing, without the need to write multithreaded code

-because operations such as filter, sorted, map, and collect are high-level artifacts that are not related to a specific threading model, their internal implementations can be single-threaded or transparent enough to take advantage of your multicore architecture! ==> in the book this sentence does not understand, the explanation is very vague !

Vegetable properties
Dish {
   name;
   Vegetarian;
   calories;
   type;
?
Dish (type) {
This . name; 
This . Vegetarian; 
This . calories; 
This . type; 
}
... getset ...
@Override
ToString () {
 name;
}
Other}
}
Small example:
Stream {
   Main (args) { 
//Menu
List<DishArrays.  Aslist (    
 Dish (Dish.  Type. meat),   
 Dish (Dish.  Type. meat),   
 Dish (Dish.  Type. meat),   
 Dish (Dish.  Type. Other ),   
 Dish (Dish.  Type. Other ),   
 Dish (Dish.  Type. Other ),   
 Dish (Dish.  Type. Other ),   
 Dish (Dish.  Type. FISH),   
 Dish (Dish.  Type. FISH));   
?
List<String//Data source   
  .  //Set up Operation line
  . Filter (x.//Remove the highest heat  
  . map (Dish:://Get the name   of the dish
  . Limit (3) //Select first three  
  . Collect (collectors.           ToList ()); //Results exist in another list inside   
System. Out . println (threehighcaloricdishnames);   
  }
}

map--accepts a lambda, transforms elements into other forms or extracts information

collect--converting a stream to another form

The difference between a stream and a collection

--a simple understanding of the ==> difference is calculated at the time

The collection ==> collection is an in-memory data structure that contains all the values currently in the data structure-each element in the collection must be counted before it can be added to the collection. (You can add or delete items to a collection, but whenever the elements in the collection are in memory, the elements have to be counted before they become part of the collection.)

Stream ==> flow is a conceptually fixed data structure (you cannot add or remove elements), its elements are calculated on demand , and from another point of view, the flow is like a collection of deferred creation: values are calculated only when required by the consumer (in the case of management, this is demand-driven , even in real-time manufacturing).

--for example, ==> collection is like when you watch a movie on a DVD, and the stream is to watch a movie online (eg: Iqiyi website)

--flow can only traverse once

Flow operations

Terminal actions: Operation to close a stream

Intermediate operation: The operation of a connected stream

List<Stringmenu  
  . Stream ()
  . filter,{ 
System. Out . println ("filter"+x);     
 x.; 
  })
  . map (x->{  
System. Out . println ("map"+x.getName ());      
 x.getName (); 
  })
  . Limit (3) 
  . Collect (collectors.  ToList ());  
System. Out . println (threehighcaloricdishnames);   
Output:
Filterpork
Mappork
Filterbeef
Mapbeef
Filterchicken
Mapchicken
[Chicken]
?
Filter and map are two separate operations, but they merge into the same traversal ===> Loop Merge technology  

Java8 the fourth 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.