Import Java.math.biginteger;import java.util.arraylist;import Java.util.arrays;import Java.util.List;import Java.util.regex.pattern;import java.util.stream.stream;/** * * @author Kangjun Ways of Creating a stream in Java 8 */publi C class Streamdemo {public static void main (string[] args) {arraysstream (); Collectionstream (); Generate (); Iteratestream (); Populastream (); }//from Arrays public static void Arraysstream () {string[] arr = {' program ', ' Creek ', ' Program ', ' Creek ', ' Java "," Web "," program "}; stream<string> stream = Stream.of (arr); System.out.println (arrays.tostring (arr)); }//from Collections public static void Collectionstream () {list<string> List = new Arraylist<> ( ); List.add ("Java"); List.add ("Python"); List.add ("C + +"); List.add ("C"); List.add ("Lisp"); stream<string> stream = List.stream (). Filter (P-P.length () > 3); string[] arr = Stream.toarray (string[]::new); System.out.println (arrays.tostring (arr)); }//use stream.generate () public static void Generate () {stream<string> Stream = Stream.generate (()-& Gt "Test"). Limit (10); string[] Strarr = Stream.toarray (string[]::new); System.out.println (arrays.tostring (Strarr)); }//use stream.iterate () public static void Iteratestream () {stream<biginteger> Bigintstream = Stream. Iterate (Biginteger.zero, N-n.add (Biginteger.ten)). Limit (10); biginteger[] Bigintarr = Bigintstream.toarray (biginteger[]::new); System.out.println (arrays.tostring (Bigintarr)); }//from Popular APIs public static void Populastream () {String sentence = "Program Creek is a Java site." ; stream<string> Wordstream = Pattern.compile ("\\w"). Splitasstream (sentence); string[] Wordarr = Wordstream.toarray (string[]::new); System.out.println (Arrays.tostring (Wordarr)); }}
Several ways to create a stream in Java8