Java 8 Stream Code Listing +API Introduction

Source: Internet
Author: User
Tags comparable java 8 stream

https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/

Construct stream

// 1. Individual valuesStream stream = Stream.of ("A", "B", "C"); // 2. Arraysnew string[] {"A", "B", "C"== arrays.stream (strarray); // 3. Collectionslist<string> List == List.stream ();

Construction of numerical flow

The numerical flow has Intstream, Doublestream, longstream three kinds, in order to reduce the cost of the carton unpacking
Intstream.of (newint[]{1, 2, 3}). ForEach (System.out::p rintln); Intstream.range (1, 3) . foreach (System.out::p rintln); intstream.rangeclosed (1, 3). foreach (System.out::p rintln);

Flow to other data structures

// 1. Arraystring[] strArray1 = Stream.toarray (string[]::new); // 2. Collectionlist<string> list1 = stream.collect (collectors.tolist ()); List<String> list2 = Stream.collect (Collectors.tocollection (ArrayList::new=  = Stream.collect (Collectors.tocollection (Stack::new)); // 3. Stringstring str = Stream.collect (collectors.joining ()). ToString (); Joining is the connection of each element of the flow, you can pass in the parameters to indicate the connection between the time the connection symbol

Operation Classification of Streams

    • Intermediate:

Map (Maptoint, FLATMAP, etc.), filter, DISTINCT, sorted, peek, limit, skip, parallel, sequential, unordered

    • Terminal:

ForEach, foreachordered, ToArray, reduce, collect, Min, Max, Count, AnyMatch, Allmatch, Nonematch, FindFirst, Findany, ite Rator

    • Short-circuiting:

AnyMatch, Allmatch, Nonematch, FindFirst, Findany, limit

Map/flatmap (map)

One-to-one mapping
list<string> output = wordlist.stream (). Map (String::touppercase). Collect (Collectors.tolist ()); List<Integer> nums = arrays.aslist (1, 2, 3, 4); List<Integer> squarenums =n * N). Collect (Collectors.tolist ());
One-to-many
Stream<list<integer>> InputStream = Stream.of (
Arrays.aslist (1), Arrays.aslist (2, 3), Arrays.aslist (4, 5, 6));
Stream<integer> outputstream = Inputstream.flatmap (Childlist-Childlist.stream ());
The so-called flat, is to reduce the dimension of the flow, each element is taken out to form a new stream, so-called to make the flow flattened

Filter

list<string> output =-word.length () > 0). Collect (Collectors.tolist ());
Here I reader is the Bufferedreader,lines method returns the data for each row as an element, note that the stream one-time feature, after consumption can not be used in the use of the file, when dealing with the relevant content to pay extra attention to

Foreach

P.getgender () = = Person.Sex.MALE)
System.out.println (P.getname ()));
The ForEach method receives a LAMBDA expression and executes the expression on each element of the Stream.
ForEach is a terminal operation, so after it executes, the elements of the stream are "consumed" and you cannot perform a two-terminal operation on a stream
Peek
perform an action on each element and return a new Streamstream.of ("One", "a", "three", "four", E.length () > 3, System.out.println ("Filtered value:" +--System.out.println ("Mapped value:" + E)). Collect ( Collectors.tolist ());

FindFirst

Short-circuiting operation, which always returns the first element of a Stream, or null. Return value type: Optional

Reduce

provides a starting value (seed), followed by an arithmetic rule (binaryoperator), combined with the first, second, nth element of the previous Stream. In this sense, string concatenation, sum of values, Min, max, average are all special reducestream.reduce (0, (A, B), A +b) equivalent to sum ()//string connection, concat = "ABCD"String concat = Stream.of ("A", "B", "C", "D"). Reduce ("", String::Concat); //minimum value, MinValue = -3.0DoubleMinValue = Stream.of (-1.5, 1.0,-3.0, 2.0). Reduce (Double.max_value, double::min); //sum, Sumvalue = 10, with a starting valueintSumvalue = Stream.of (1, 2, 3, 4). Reduce (0, integer::sum);//sum, Sumvalue = 10, no starting value, this returns the optional typeSumvalue = Stream.of (1, 2, 3, 4). reduce (integer::sum). get ();//filtering, string connection, concat = "Ace"Concat = Stream.of ("A", "B", "C", "D", "E", "F"). Filter (xX.compareto ("Z") > 0). Reduce ("", String::Concat);

Limit/skip

Limit returns the first n elements of a Stream, and skip discards the top n elements. Limit and skip to sorted after the number of runs have no effect, that is, you do not know the results of sorted you limit no use, do not know where to limit or the same sort

Sorted

list<person> personList2 = Persons.stream (). Limit (2). Sorted ((P1, p2), p1.getname (). CompareTo ( P2.getname ())). Collect (Collectors.tolist ()); Comparable is not implemented to specify comparable (LUMBDA expression)



Java 8 Stream Code Listing +API Introduction

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.