Java 8 Stream Try

Source: Internet
Author: User
Tags iterable java 8 stream

As the project has a demand, what needs? Well, it is necessary to run a iterable in multicore state. The existing scenario is that wrapper iterable, which encapsulates the iterable, takes the Iterable object as a private member of the encapsulated class, and then writes a Synchronized method or Synchronized block for it. Implements the unique access rights to the next () function. Synchronized block is recommended here because the former will bring a DOS problem (how?). It is stated in the article)

class a{   iterable<T> iter;     lock;  Public Next ()    {      T result;      Synchronized (lock)                {               Tnull;        }             return T;
}}

There may be grammatical errors in the above code, but the approximate meaning is expressed, time is limited, and later supplements.

However, the above problem only solves the thread safety of iterable under multithreading. The issue of multi-core simultaneous access itreable is not addressed. Google search, many of the recommended use of Java 8 stream parallel. Oh, I'm so ignorant. Well, start learning what the hell the stream is.

First of all, using the one hours before bedtime, Chinese and English blogs at the same time, read the approximate, understand the stream of data sources, intermediate conversion and termination status problems. I have a rough idea of what's going on. But has been tangled, can you use your own defined type (class), could be used to define their own functions, how to use? To sleep before thinking, summed up the experience of the front to see MVN-not always look at the online blog introduction, because the system, can only be seen in the tube, can not form a holistic cognition, it depends on the official manual or textbooks. Rest assured that you'll sleep. However, getting up early in the morning found that there was no such information online at all. Well, look at blogs, and start writing some simple programs according to your needs. On the program:

Stream_hello.java
Package Com.mkyong.java8;import Java.util.stream.collectors;import Java.util.arrays;import java.util.Map;import Java.util.list;import Java.util.function.function;import Java.util.collection;public class Stream_hello{private Static Final class Task{private final Integer points; Task (final Integer points) {this.points=points;} Public Integer getpoints () {System.out.println ("threadid=" +thread.currentthread (). GetId () + "" +points); return points ;} Public Integer println () {System.out.println (); return points;} Public String toString () {return String.Format ("[%d]", points);}} public static void Main (String [] args) {System.out.println ("single Thread Stream-list"); List<string> items=arrays.aslist ("Apple", "apple", "banana", "apple", "orange", "banana", "papaya"); Map<string, Long> Result=items.stream (). Collect (Collectors.groupingby (), Collectors.counting ())); SYSTEM.OUT.PRINTLN (result); System.out.println (); SYSTEM.OUT.PRINTLN ("Single Thread Stream-task"); final collection<task> Tasks=arrays.aslist (New Task (5), new Task (4), new Task (5)), Final long Totalpoint=tasks.stream (). Filter (task-> Task.getpoints () >4). Filter (Task->task.println () >4). Maptoint (task::getpoints). sum (); System.out.println ("Total points:" +totalpoint); System.out.println (); System.out.println ("Multi thread"), Final collection<task> tasks2=arrays.aslist (new Task (5), new Task (4), new Task (5)); final long Totalpoint2=tasks2.stream (). Parallel (). Map (Task->task.getpoints ()). Reduce (0,integer::sum) ; System.out.println ("Total points:" +totalpoint2); System.out.println ();}}

Compile as follows: Javac Stream_example.java get Stream_example.class

Run as follows: Java stream_example

Operation Result:

Single thread stream-list{papaya=1, orange=1, banana=2, Apple=3}single thread stream-taskthreadid=1  5threadid=1  5threadid=1  4threadid=1  5threadid=1  5Total points:10multi threadthreadid=10  5threadid=9  5threadid=1  4Total points:14

With the above code, there is a stream of string, a stream of its own defined class as an element, and a multi-core stream of its own defined class as an element.

My current cognition is as follows: You can define the element itself, define the element's data member, define the method of the element, and the return result of the method can be used as the reference standard of Filter,map function.

The filter, map, and other functions are converted to stream according to certain conditions.

PS: Given some knowledge, I would like to share with you. So, some blogs I will post in the essay.

Java 8 Stream Try

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.