10 Ways to Create a Stream in JavaCollections of objects can be processed using the Stream API, which was first released in Java 8. A stream is a collection of items that can be pipelined in a variety of ways to achieve distinct outcomes. Java Stream's characteristics are:
The different approaches for creating the streams: Approach: Using the CollectionsAlgorithm: Step 1: Obtain the collection Step 2: Use the Collection.stream() function to create a Sequential Stream from the collection. Step 3: Print the Stream Implementation:FileName: SequentialStream.java Output: Hello Welcome to the World! Approach: Create a stream from specified valuesThe t values can be used to build a stream using the Stream.of(T…t) method, where t are the elements. A sequential Stream with the t elements is the result of this method. Implementation:FileName: StreamSpecifiedValues.java Output: 11 22 33 44 55 66 77 88 99 Approach: Create a stream using Arrays.stream()A sequential stream can be created from a specified array using the Stream.of() and Arrays.stream() methods, which are often used. When a non-primitive type T is used to invoke either of these methods, a Stream is returned. Implementation:FileName: ArrayStream.java Output: Hello Welcome to World! Approach: Create a stream using Stream.of()A non-interfering action that returns a new stream while also being applied to components as they are taken in from the stream. Implementation:FileName: StreamOFCreation.java Output: Hello Welcome to World! Approach: Create a Stream using Stream.builder()The build() method will generate an instance of the Stream; otherwise, the builder() method is used when the desired type needs to be explicitly given in the correct part of the statement. Implementation:FileName: StreamStringBuilder.java Output: Hello Welcome to the World! Approach: Create an infinite Stream using Stream.iterate()An infinite sequential ordered stream is returned by the iterate() method, which is created by repeatedly applying a function f to a seed element. The first parameter of the iterate method is the first element of the resultant stream in the example below. The function is applied to the preceding element in order to create each subsequent element. Implementation:FileName: IterateStream.java Output: 3 9 81 6561 43046721 -501334399 2038349057 Approach: Create an infinite Stream using Stream.generate() methodThe generated stream is limitless and can be generated by the generate() method, which takes as a given input. Hence, to limit it, either give the size we want or let the generate() method run until the memory limit is reached. Implementation:FileName: GenerateStream.java Output: 0.8285302774864066 0.6259903293195835 0.28498534663189434 0.06681813318465979 0.6526779164446666 0.36696884550384856 Approach: Create a stream from a Pattern using a PredicateA boolean-valued predicate function is created in Java 8 through the Predicate asPredicate() method of Pattern and is utilized for pattern matching. Implementation:FileName: StreampatternPredicate.java Output: to the Approach: Create a stream from the IteratorThe Collection Framework uses Java iterators to get elements one at a time. The sequential stream can only be created with the help of the splitterator. Thus, a Spliterator is additionally used in this technique. However, in this case, an Iterable that is created from the Iterator is used as the Spliterator's source. Therefore, the Iterator is used to build the Iterable first. Next, the Spliterator is given directly as Iterable.spliterator() to the stream() method. Implementation:FileName: IteratorStream.java Output: Hello Welcome to World! Approach: Create a stream from IterableSince iterable interfaces are not meant to have their own stream() method, they are designed with this in mind. All that needs to be done is pass it into the StreamSupport.stream() function to obtain a stream from the supplied Iterable object. Converting an Iterable to a Stream is simpler. Spliterator(), the default method of Iterable, can be used to obtain an instance of Spliterator, which can then be transformed into a Stream. Note: The Iterable is not an instance of Collection; rather, this method calls the Collection.stream() method internally when it has to obtain a sequential stream from the Spliterator. Otherwise, it is called StreamSupport.stream().Implementation:FileName: IterableStream.java Output: Hello Welcome to World! |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India