Collection From a Stream

Java-Collection From a Stream

Collection From a Stream

The stream API provides the collect( ) method. This method is used to obtain a collection from a stream. The ability to move data from a collection to a stream, and then back to a collection again is a very powerful attribute of the stream API. It gives you the ability to operate on a collection through a stream, but then repackage it as a collection. The collect( ) method has two forms. The first is:

Collection From a Stream

Here, supplier specifies how the object that holds the result is created. For example, to use a LinkedList as the result collection, you would specify its constructor(LinkedList::new) or its object(()->new LinkedList<>()). The supplier, accumulator, and combiner functions are work only if the stream is parallel. If the stream is sequential the supplier and the accumulator functions are only work, the combiner function is ignored.

Collection From a Stream

The accumulator function adds an element to the result and combiner combines two partial results. Thus, these functions work similarly to the way they do in reduce( ). For both, they must be stateless and non-interfering. They must also be associative.

Note that the supplier parameter is of type Supplier. It is a functional interface declared in java.util.function. It specifies only the get( ) method, which has no parameters and, in this case, returns an object of type R. Thus, as it relates to collect( ), get( ) returns a reference to a mutable storage object, such as a collection. Note also that the types of accumulator and combiner are BiConsumer. This is a functional interface defined in java.util.function. It specifies the abstract method accept( ). The following example shows how this method works on parallel stream. It uses the LinkedList as the supplier:

Collection From a Stream

Notice that the first argument to collect( ) is a lambda expression that returns a new LinkedList. The second argument uses the standard collection method add( ) to add an element to the list. The third element uses addAll( ) to combine two linked lists. Notice the execution, the multiple linked lists are created by supplier(because of multiple threads). Next, each element in the stream are added to the each list by add( ). Finally that lists are combined using the addAll(). The execution order may change each and every time(because of multiple threads). The following example shows how this method works on sequential stream. It uses the LinkedList as the supplier:

Collection From a Stream

In this example, the strstm is a sequential stream, so the combiner function is ignored. Notice the execution, only one Linked List is created by supplier. That list is filled by the stream elements by accumulator function. It’s not always necessary to specify a lambda expression for the arguments to collect( ). Often, method and/or constructor references will suffice. For example:

Collection From a Stream

This statement creates a LinkedList that contains all of the elements in the strstm stream. Notice that the first argument specifies the LinkedList constructor reference. The second and third specify method references to LinkedList’s add( ) and addAll( ) methods. In the language of the stream API, the collect( ) method performs what is called a mutable reduction. This is because the result of the reduction is a mutable (i.e., changeable) storage object, such as a collection.

Another Example

Collection From a Stream


Program

Collection From a Stream

Program Source

import java.util.stream.Stream;
import java.util.LinkedList;

public class Javaapp {

    public static void main(String[] args) {

        String strary1[] = {"111", "222", "333", "444", "555"};
        Stream<String> strstm = Stream.of(strary1);
        LinkedList<String> li = strstm.parallel().collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
        System.out.println("li-> " + li);
        
        String strary2[] = {"AA", "BB", "CC", "DD", "EE", "FF"};
        strstm = Stream.of(strary2);
        StringBuilder stb;
        stb = strstm.collect(StringBuilder::new, StringBuilder::append, (u, t)->{});
        System.out.println("stb-> " + stb); 
    } 
}

Leave a Comment