Java 8 Stream Application Scenario

Source: Internet
Author: User
Tags java 8 stream

Stream is a big improvement in Java 8. The function of stream is to support various operations of the collection, such as filter, SUM, max, Min, average, map, reduce and so on. So I personally think that the stream's appearance is based on the following reasons:

    • Enhanced collection operations
    • Embracing functional programming
    • Take full advantage of lambda
    • Improved execution efficiency-transparent support for multithreaded collection operations

The author tries to test the power of stream concurrency, and finds that the stream concurrency process has no advantage over the traditional for-each loop in the face of a particularly simple task. It seems that stream is not a free lunch, and there is some overhead in creating the stream . So this prompted the author to think about what scenario to use stream.

Test examples

The author tests two examples, one task is very simple, the other one is slightly more complicated. From the results, parallel stream is always faster than serial, simple task, for loop faster, more complicated task, parallel stream comes from behind, the improvement of parallelism is enough to cover the cost of creating stream.

Test Gadget Class
public class TimeRecorder {    private long startTime;    private long endTime;    public void start() {        startTime = System.currentTimeMillis();    }    public long end() {        endTime = System.currentTimeMillis();        return endTime - startTime;    }    public long getDuration() {        return endTime - startTime;    }}
A very simple example of a task

Only call Intvalue such a small method.

  public class Streamdemosimple {public static void main (string[] args) {list<integer> intlist        = new Linkedlist<integer> ();        for (int i = 1; I <= 1000000; i++) {intlist.add (i);        } timerecorder Recorder = new Timerecorder ();        Recorder.start ();        Intlist.stream (). ForEach (I-, {i.intvalue ();        });        Recorder.end ();        System.out.print ("Stream iterator:");        System.out.println (Recorder.getduration ());        Recorder.start ();        Intlist.parallelstream (). ForEach (I-, {i.intvalue ();        });        Recorder.end ();        System.out.print ("Parallel Stream iterator:");        System.out.println (Recorder.getduration ());        Recorder.start ();        for (Integer i:intlist) {i.intvalue ();        } recorder.end ();        System.out.print ("Normal iterator:");    System.out.println (Recorder.getduration ()); }}

The output is as follows:

Stream iterator:447
Parallel Stream iterator:142
Normal iterator:70

A slightly more complex example of a task

Take a few more steps.

public class Streamdemo {public static void main (string[] args) {list<integer> intlist = new linkedlist&        Lt;integer> ();        for (int i = 1; I <= 1000000; i++) {intlist.add (i);        } timerecorder Recorder = new Timerecorder ();        Recorder.start ();            Intlist.stream (). ForEach (I-, {i.intvalue ();            I.intvalue ();            i.ToString ();            I.intvalue ();            I.intvalue ();        i.ToString ();        });        Recorder.end ();        System.out.print ("Stream iterator:");        System.out.println (Recorder.getduration ());        Recorder.start ();            Intlist.parallelstream (). ForEach (I-, {i.intvalue ();            I.intvalue ();            i.ToString ();            I.intvalue ();            I.intvalue ();        i.ToString ();        });        Recorder.end ();        System.out.print ("Parallel Stream iterator:");        System.out.println (Recorder.getduration ()); Recorder. Start ();            for (Integer i:intlist) {i.intvalue ();            I.intvalue ();            i.ToString ();            I.intvalue ();            I.intvalue ();        i.ToString ();        } recorder.end ();        System.out.print ("Normal iterator:");    System.out.println (Recorder.getduration ()); }}

The output is as follows:

Stream iterator:808
Parallel Stream iterator:313
Normal iterator:377

Stream's Fit scene
    • more than two steps in a collection operation
      For example, filter first
      The stream appears elegant, concise, and efficient.
    • task is heavier, focus on performance, hope to execute concurrently
      It is easy to use multithreading technology implicitly. Very good time to use.
    • in the coding style of functional programming
      One of the original designs of stream
Conclusion

Lambda,stream, etc. new features make Java functional programming more natural. The right environment is well worth the reasonable use. But keep in mind that stream creation and transmission are also lossy, and that a particularly simple scenario might be more suitable for a traditional for loop.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java 8 Stream Application Scenario

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.