Java8 new Features--stream

Source: Internet
Author: User
Tags stream api

First, create stream
  1. Create stream mode one: Stream () or Parallelstream () of the collection class

    java List<String> list = new ArrayList<>(); Stream<String> stream = list.stream();

  2. Create stream mode two: Get through the static method stream () in arrays

    new String[10];Stream<String> stream1 = Arrays.stream(strings);
  3. Create stream mode three: by static method in the Stream class of ()

    Stream<String> stream2 = Stream.of("aa""bb""cc");
  4. Create stream mode four: infinite stream
    Iteration:

    Stream<Integer> stream3 = Stream.iterate(02);

    Generated:

    Stream.generate(() -> Math.random());
Second, intermediate operation
    • Filtering and slicing
      Filter accepts a lambda and filters out the element that satisfies the condition from the stream.
      Limit limits the number of elements in the Stram. After the limit quantity is met, later iterations in the stream terminate, similar to the short-circuit operation.
      Skip (n) skips the element and returns a stream that throws out the first n elements. If there are less than n elements in the stream, an empty stream is returned. Complementary to limit (n)
      Distinct removes duplicate elements through Hashcode () and Equels () of elements in stream

    • Mapping
      Map accepts lambda, transforms elements into other forms or extracts information. Takes a function as an argument, and the function is applied to each element, and it is mapped to a new element.
      FlatMap takes a function as an argument, replaces each value in the stream with another stream, and then connects all the streams into a stream.

    • Sort
      Sorted () Natural sort sorted by comparable
      Sorted (Comparator com) custom sort sort by Comparator

    • Return to about
      t reduce (t identity, binaryoperator

    • Collect
      Optional
    1. Internal iterations: Iterative operations are done by the stream API
    2. External iterations: Write your own iteriator
Third, parallel stream and serial stream

A parallel stream is one that divides a piece of content into chunks of data and processes each stream of data blocks separately with different threads. Java 8 optimizes parallelism, and we can easily parallelize the data. The stream API can declaratively switch between a parallel stream and a sequential stream through parallel () and sequential ().

Java8 new Features--stream

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.