Introduction to Java Collection Stream
A Java Stream is one of the collection components and it’s the API for iterating its items internally and that is, it can iterate its own elements if we want to use the Java Collections iteration features, such as a Java Iterator or the Java for-each loop with a Java iterable interface it must be implemented the element iteration the streams do not change the value in the data structure memory and it will provide the results with using default methods which we can allocate the default space like pipelined structure for to produce the desired outputs.
Guide to Java Collection Stream
First which is probably and most argument for introducing a stream method to the collection interface. Once the list of items is shown the stream data will be opened with a specific stream. The simplest and most popular action is forEach() loop, which runs through the stream elements and calls the given function on each one. This method is also to use widely and that it has been included in Iterable, Map, and other classes. Streams are nothing more than data wrappers, they don’t want to store the data or affect the underlying data source. Many useful and high-performance operations are supported by streams, which are described succinctly with lambdas that can be done sequentially or in parallel. A stream is not a data structure because it does not store any data. It also doesn’t make any changes to the underlying data source. It doesn’t matter if our data is processed sequentially or not sequence order if the data is in parallel with our Stream is ordered; the implementation will keep the Stream’s should be the encounter order. Generally, the stream is thread-safe if the Spliterator used has the CONCURRENT property. The stream API defines a number of contracts for each step of the pipeline, and if any of them are broken, unexpected behaviour or exceptions may occur.
Stream instance from a Collection instance
The Streams takes the input from the Collections, Arrays and other Input/Output channels these are the ways for passing the input to the function. With the help of a new and object creation procedure, the object will be created and allotted for the specific function call. Also, the Streams only provided the output results as per the pipelined architecture and its methods it will not change the value and the real data structure process. Also, the collections are mainly iterated with the help of other and external loops but when we use to stream it will need to be iterated internally with the help of specific loop operations mostly it does not need the external loops. One of the terminal methods of the Stream API in Java 8 is collect() method for collecting the data. It enables us to conduct mutable fold operations using data components contained in a Stream instance (e.g., repackaging elements into data structures and applying additional logic, concatenating them, etc.). In the Collectors class, you’ll find all predefined implementations. To benefit from better readability, it’s usual practice to use the following static import. To aggregate all Stream items into a List object, use the toList collector. The crucial thing to know is that with this technique, we can’t presume any certain List implementation. We can use toCollection instead if we want greater control over this. To aggregate all Stream items into a Set instance, use the toSet collector. It’s vital to realise that using this method, we can’t presume any certain Set implementation. We can use a collection instead if we want greater control over this. There are no duplicate elements in a Set. If our collection contains components that are identical, they will only appear once in the final Set. To aggregate Stream items into a Map instance, use the toMap collector. To do so, we’ll need two functions: keyMapper and valueMapper.To extract a Map key from a Stream element, we’ll use keyMapper, and to retrieve a value associated with a specific key, we’ll use valueMapper.
Java Stream Creation
In java different ways to create Streams like by using collection, creating stream data from specified values. It also creates an array using the stream data it calculates the empty spaces with the help of Stream. empty() method not only calculates empty spaces we can create the Stream data using Stream. builder() method. With the help of iterate() method, we can calculate the infinite Stream data because the Iterable is the interface and it does not provide any default method StreamSupport.Stream() method and get a Stream data using Iterable object.
Example #1
import java.util.*;
import java.util.stream.*;
class first {
private static <T> void methd(Iterable<T> itr)
{
Stream<T> str
= StreamSupport
.stream(itr.spliterator(),
false);
Iterator<T> its = str.iterator();
while (its.hasNext()) {
System.out.print(its.next() + " ");
}
}
public static void main(String[] args)
{
Iterable<String> itr
= Arrays.asList("Marina", "Adyar", "Parrys or Broadway","Tambaram","Gundy");
methd(itr);
}
}
Sample Output:
In the above example we used Stream Instance creation and also we can use the StreamSupport class for accessing the stream() method. Like that we can use the Iterator<> interface for accessing the stream inputs and assign it to the variable. In the main method using Iterable<> interface we can access the values as Arrays and asList() method.
Example #2
import java.util.*;
import java.util.stream.*;
class Second {
private static <T> void methd(Iterator<T> itr)
{
Spliterator<T> splititr
= Spliterators
spliteratorUnknownSize(itr,
Spliterator.NONNULL);
Stream<T> str
= StreamSupport.stream(splititr, false);
Iterator<T> its = str.iterator();
while (its.hasNext()) {
System.out.print(its.next() + " ");
}
}
public static void main(String[] args)
Iterator<String> itre = Arrays
.asList("Thiruvanmaiyur", "Perungalathur", "TNagar","Central","Besant Nagar")
.iterator();
methd(itre);
}
}
Sample Output:
In the above example, we used an additionally Spliterator class for accessing the Stream inputs using Iterator interface we can access the Array List inputs using the asList() method.
Conclusion
Recommended Articles
This is a guide to Java Collection Stream. Here we discuss the Introduction, Stream instance from a Collection instance, examples with code implementation. You may also have a look at the following articles to learn more –