Java Learning (12)

Source: Internet
Author: User

/**************************************************************/

/* Updating the graphical interface in the thread requires calling Swingutilties.invokelater.

/* Specific examples are as follows:

/**************************************************************/

A streaming operation that supports the functional operation of numeric values in Java. Streaming does not have anything to do with data flow, which is primarily list and lambda expressions, and the latter is input and output, and so on.

The operation of stream has two types, the first is intermediate operation, the result is the flow data, and the second is the end operation, and the resulting data is the output character.

Intermediate Operation:

Filter: Excludes all elements in the stream that do not conform to the condition/assertion

Map: Map The elements of a stream one by one

*flatmap: Converting each element to an element with no or more

*peek: Take some action on each element encountered

Distinct: Excluding the exact same data in the data stream

Sorted: A sort operation on a data stream

Limit: Limit the first n data in a data stream

Substream: Qualifying the first n data in a data stream based on index

Skip: Ignore data that matches the condition/assertion

Maptodouble/maptoint/maptolong: Type Conversion

End Operation:

ForEach: Retrieving data from a stream one by one

ToArray: Entering data from a stream into an array

Min: Find minimum value

Max: Ask for maximum value

Count: Calculate the number of data in the stream

AnyMatch: Determine if there is at least one element match condition/assertion in the stream

Allmatch: Determine if all element pairing conditions/assertions are in the stream

Nonematch: Determine if there are no element pairing conditions/assertions in the stream

FindFirst: Finding the first element in a stream

Findany: Finding any element in a stream

Average: Averaging

The operation of the flow is generally divided into three steps: to get the data flow, the middle operation of convection, the end operation of convection, the result is obtained. There are several types of streams: array arrays, collection collection (including list), mapping map. The resulting streams are in the form Array.stream (array), Collection.stream (). Map does not have a stream, but provides several ways to get the flow: Map.putlfabsent, Map.computelfpresent, Map.merge.

Stream also has the following common sub-interfaces, such as Intstream, Doublesream, Longstream, stream<t>.

Stream is easy to convert from a serial operation to a parallel operation, with only the stream () changed to Parallelstream (), which can greatly improve the operational efficiency for multicore computers. Very convenient to implement.

There are three forms of array/string conversion to Data flow: Arrays.stream (Array), Stream.of (array), Arrays.aslist (array). Stream (). Specific as follows

1       Public voidTostream ()2     {3Stream<integer> Streami=stream.of (1,2,3,4,5);4Streami.foreach (X->system. out. Print (x+" "));5System. out. println ();6         7String[] str={"Andy","Anny","Elizebath","Jane"};8Stream<string> streams=stream.of (str);9Streams.foreach (X->system. out. Print (x+" "));TenSystem. out. println (); One          AArrays.stream (str). ForEach (X->system. out. Print (x+" ")); -System. out. println (); -          theArrays.aslist (str). ForEach (X->system. out. Print (x+" ")); -System. out. println (); -          -List<string> List=arrays.aslist ("Hello"," World","!"); +List.stream (). ForEach (X->system. out. Print (x+" ")); -System. out. println ();  +}

The following example shows a list type as an example of a convection operation function.

The stream data is stored using the function collect (Collectors.aslist ()).

1List<integer> List=arrays.aslist (6,2,3,4,4,5,1,7,8,9);2List<integer> Tempint=list.stream ()//take a value greater than 53. Sorted ()//sort from small to large4. DISTINCT ()//Remove Duplicate Values5. Limit (8L)//take the first three values6. Filter (x->x<8)7. Sorted ()//sort by from small to large8. Map (X->x*x*x)//mapped to three-time square value9. Collect (Collectors.tolist ());

1.min ()/max () operation to find the minimum/maximum data in the data stream. The control output maximum or minimum value is freely controlled by the user, regardless of the function name. The value type returned is optional. The output can be obtained through the Get () function.

1 optional<integer> min; 2 optional<integer> max; 3 Min=tempint.stream (). Min ((n1,n2)->n1-n2); 4 Max=tempint.stream (). Max ((N1,N2)->n1-n2);

Get the right results. However, change Max to

Max=tempint.stream (). Max ((N1,N2)->n2-n1);

The minimum result will be obtained.

2.count () operation to calculate the number of elements in the data stream.

1 long count=0; 2 Count=tempint.stream (). Count ();

3.allMatch () to determine whether all elements meet the criteria. Yes, then return True

AnyMatch () to determine if there are elements that satisfy the condition. Yes, then return True

Nonematch () to determine whether all elements satisfy the condition. No, it returns true

1         Boolean ball=false; 2         Boolean bany=false; 3         Ball=tempint.stream (). Allmatch (x->x==8); 4         Bany=tempint.stream (). AnyMatch (x->x==8);        

4.findAny () to find if the stream has element 2. If so, the first element is returned

FindFirst () to find out whether the first element in the stream exists. If yes, the first value is returned

1         Optional<integer> FindFirst; 2         Optional<integer> Findany; 3         findfirst=List.stream (). FindFirst (); 4         Findany=list.stream (). Findany ();

5. String Case Conversion

1List<string> Str=arrays.aslist ("Hello"," World","!");2List<string>tempstr=Str.stream ()3. Map (String::touppercase)//String Uppercase4. Map (String::tolowercase)//String Lowercase5 . Collect (Collectors.tolist ());6 7Tempstr.stream (). ForEach (X->system. out. Print (x+" "));8System. out. println ();

Java Learning (12)

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.