Stream of Java (JDK8)

Source: Internet
Author: User
Tags new set

First, examples

Importjava.util.Arrays;Importjava.util.List;Importjava.util.stream.Collectors;ImportJava.util.stream.Stream;/*** Created by Root on 16-5-19.*/ Public classStreamtest {/*** Set of not less than 50 of the number output out [1, ,,,,, 185, Max.*/    Private Static voidfun1 () {int[] A = {1, 20, 63, 58, 185, 60, 59, 20}; Arrays.stream (a). Filter (x(x >=50) . ForEach (system.out::p rintln); }    /*** Use Stream's own properties to determine if the following collections are all larger than 50,[1, (+, +, +-185, +/-)*/    Private Static Booleanfun2 () {int[] A = {1, 20, 63, 58, 185, 60, 59, 20}; returnArrays.stream (a). Allmatch (P, p > 50); }    /*** Output The same string that exists in the following two collections, ["Qwer", "asdf", "ZXCV", "FGH", "Jum", "RFV"],["EDC", "Zse", "asdf", "ZXCV", "Yhnj"]*/    Private Static voidFun3 () {string[] S1= {"Qwer", "asdf", "ZXCV", "FGH", "Jum", "RFV"}; string[] S2= {"EDC", "Zse", "asdf", "ZXCV", "Yhnj"}; Arrays.stream (S1). ForEach (x-{arrays.stream (S2). Filter (Y-y.equals (x)). ForEach (System.out::p rintln);    }); }    /*** Multiply all the numbers in the following set by 10, and get a new set [1, +,, +, 185,*/    Private Static voidFun4 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Map (n-n *). ToArray (integer[]::New);    Arrays.stream (num). ForEach (System.out::p rintln); }    /*** Remove the 3rd to 7th value from the following set, plus 5, to get a new set [1, ten, seven, seven, 185, X, Min, +]*/    Private Static voidfun5 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Skip (2). Limit (5). Map (x, x+5). ToArray (integer[]::New);    Arrays.stream (num). ForEach (System.out::p rintln); }    /*** Output The minimum value of the following set [1, 185, Min .,.*/    Private Static voidfun6 () {int[] Nums = {1, 20, 63, 58, 185, 60, 59, 20};    System.out.println (Arrays.stream (nums). Min (). Getasint ()); }    /*** Sort from small to large for the following collections [1, +, +, 185, +,--]*/    Private Static voidfun7 () {int[] Nums = {1, 20, 63, 58, 185, 60, 59, 20};    Arrays.stream (nums). Sorted (). ForEach (System.out::p rintln); }    /*** Collection Merge: ["Qwe", "DFG", "SSS"],["Qwe", "DFG", "SSS"],["Qwe", "DFG", "SSS"] * merged into ["Qwe", "DFG", "SSS", "Qwe", "DFG", "SSS", "Qwe", "DFG", "SSS"]*/    Private Static voidFun8 () {Stream<List<String>> InputStream =Stream.of (Arrays.aslist ("Qwe", "DFG", "SSS"), Arrays.aslist ("Rty", "ghkj", "Fghh"), Arrays.aslist ("GTB", "SSD", "DG")        ); Stream<String> outputstream = Inputstream.flatmap (P-P.stream ()); List<String> str =Outputstream.collect (Collectors.tolist ());    System.out.println (str); }    /*** Take out the following set of more than 50 and get a new set [1, +, +,, 185, +,--]*/    Private Static voidFun9 () {integer[] nums= {1, 20, 63, 58, 185, 60, 59, 20}; integer[] Num= Arrays.stream (nums). Filter (P > P). ToArray (integer[]::New);    Arrays.stream (num). ForEach (System.out::p rintln); }    /*** Convert the following names to uppercase, then sort the output ("Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell")*/    Private Static voidFun10 () {string[] str= {"Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell"}; Arrays.stream (str). Map (P-p.touppercase ()). Sorted (). ForEach (System.out::p rintln); }     Public Static voidMain (string[] args) {fun8 (); }}

Stream of Java (JDK8)

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.