Java8 new characteristic--flow interface Streams__java

Source: Internet
Author: User

Today is the day of the college entrance examination, is senior elder brother and sister to reply graduation day. A generation comes again, and a generation goes. For a long time did not write a blog, to take this special day to tidy up a short time ago to learn java8 new features written code, leaving traces. (This blog's code according to java8 new characteristics of the tutorial learning to organize, coupled with personal understanding, about a new feature of the introduction code in the comments have been elaborated, so no longer write text introduction, directly look at the code.) )


This article describes one of the new features of JAVA8: streaming interface streams.

Import java.util.ArrayList;
Import java.util.List;

Import java.util.Optional;

Import Org.junit.Test; The/** * Flow interface (Streams) Java.util.Stream represents a string of elements on which you can perform multiple operations.
	The flow operation can be either continuous or interrupted. The interrupt operation returns the result of the operation.
	The continuous operation returns the stream itself so that you can call the chain method on the line. Streams are created on the data source, for example: Java.util.Collection, List collection, and set collection (map not supported).
	Flow operations can be executed sequentially or in parallel. We first understand how the flow of the next sequence works.
 We first create a string list.
	
	* * public class Streamdemo {static list<string> StringCollection = new arraylist<> ();
		static{Stringcollection.add ("ddd2");
		Stringcollection.add ("Aaa2");
		Stringcollection.add ("BBB1");
		Stringcollection.add ("Aaa1");
		Stringcollection.add ("Bbb3");
		Stringcollection.add ("CCC");
		Stringcollection.add ("Bbb2");
	Stringcollection.add ("Ddd1");
	The collections class for//JAVA8 has been extended, and you can simply invoke Collection.stream () or Collection.parallelsteam () to create the stream.
	
	The following sections describe most of the flow operations. /** * Filter accepts a predicate to filter all elements in the stream.
	 This operation is continuous, and it allows us to continue to invoke another stream operation foreach on the result. * foreach accepts a consumer, which is used to perform operations on each element in the filtered stream. 
	 foreach is an interrupt operation.
	 * foreach returns void, so we cannot invoke other stream operations after foreach. * * @Test public void Filter () {stringcollection. Stream (). Filter ((s)->s.startswith ("a")). ForEach (System.ou
		T::p rintln); 
	 "Aaa2", "AAA1"}/** * Sorted is a intermediate operation which returns a Sorted view of the stream.
	 * The elements are sorted in natural order unless your pass a custom Comparator. */@Test public void Sorted () {stringcollection. Stream (). Sorted (). Filter ((s)-> S.startswith ("a")). Fo
		Reach (System.out::p rintln); "Aaa1", "Aaa2"//keep in mind then sorted does only create a sorted view of the stream//without manipulating
		The ordering of the backed collection. The ordering of StringCollection is untouched:System.out.println (stringcollection);
		[Ddd2, Aaa2, BBB1, AAA1, Bbb3, CCC, BBB2, DDD1]}/** * Map continuity Operations Map converts each element in the stream to another object through the specified function.
		The following example converts each string to an uppercase string.
		Alternatively, you can use map to change the type of each element to another type.
	 The generic type of the converted stream depends on the generic type of the function you are passing in.
		*/@Test public void Map () {stringcollection. Stream (). Map (String::touppercase). Sorted ((A, B)-> B.compareto (a)). ForEach (System.out::p rint); "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"}/** * Various matching operations can be used to detect whether a certain predicate matches elements in the stream.
	 All of these operations are interrupted and a Boolean result is returned. */@Test public void Match () {Boolean anystartwitha = Stringcollection.stream (). AnyMatch ((s)-> S.startswith ("
		A ")); System.err.println (Anystartwitha);
		True Boolean Allstartwitha = Stringcollection.stream (). Allmatch ((s)->s.startswith ("a")); System.err.println (Allstartwitha);
		False Boolean Nonestartwitha = Stringcollection.stream (). Nonematch ((s)-> s.startswith ("z")); System.err.println (Nonestartwitha);
	 True}/** * Count Count is an interrupt operation that returns the number of elements in the stream. */@Test public void Count () {Long STARTWITHB = StringCollection. Stream (). Filter ((s)->s.startswith ("
		B ")). Count (); System.err.println (STARTWITHB); 3}/** * Reduce * This disruptive operation uses the specified function convection element to implement the mitigation strategy. The return value of this operation is one that includes allThe optional of the element that is being subtracted.
				*/@Test public void Reduce () {optional<string> Optional = StringCollection. Stream (). Sorted ()
		
		. reduce ((S1, S2)->s1+ "#" +S2);		
		Optional.ifpresent (System.out::p rint); AAA1#AAA2#BBB1#BBB2#BBB3#CCC#DDD1#DDD2}}

For more information, see this blog: java8 new Features tutorial


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.