Metoda Stream of() în Java

Flux de (T t)

Flux de (T t) returnează un flux secvenţial care conţine un singur element.
Sintaxa:

 static Stream of(T t) 

Parametri: Această metodă acceptă un parametru obligatoriu t care este singurul element din Flux.

Valoare returnată: Stream of(T t) returnează un Stream secvenţial care conţine singur element specificat.

Exemplu:




// Java code for Stream of(T t)> // to get a sequential Stream> // containing a single element.> > import> java.util.*;> import> java.util.stream.Stream;> > class> GFG {> > > // Driver code> > public> static> void> main(String[] args)> > {> > // Creating an Stream having single element only> > Stream stream = Stream.of(> 'Geeks'> );> > > // Displaying the Stream having single element> > stream.forEach(System.out::println);> > }> }>

Ieșire:

 Geeks 

Flux de (valori T...)

Flux de (valori T...) returnează un flux ordonat secvenţial ale cărui elemente sunt valorile specificate.

Sintaxa:

 static Stream of(T... values) 

Parametri: Această metodă acceptă un parametru obligatoriu valorile care sunt elementele noului flux.

Valoare returnată : Stream of(T... values) returnează un flux ordonat secvenţial ale cărui elemente sunt valorile specificate.

Exemplu:




// Java code for Stream of(T... values)> // to get a sequential ordered stream whose> // elements are the specified values.> > import> java.util.*;> import> java.util.stream.Stream;> > class> GFG {> > > // Driver code> > public> static> void> main(String[] args)> > {> > // Creating an Stream> > Stream stream = Stream.of(> 'Geeks'> ,> 'for'> ,> 'Geeks'> );> > > // Displaying the sequential ordered stream> > stream.forEach(System.out::println);> > }> }>

Ieșire:

 Geeks for Geeks