Sitemap

collect Stream Element: collect(..) method in Java Stream

1 min readAug 21, 2023

--

collect(…) method is terminal operation in java stream.

it will return collection of data from a given Stream.

in simply word we can say that it used to collect data from Stream in form of collection(set/list/map)

<R,A> R  collect(Collector<? super T,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a Collector.
String names[]={"Ram","Shyam","Mohan","BhanuPratap"};

List<String> collectNames=Arrays.stream(names)
.collect(Collectors.toList());

System.out.println(collectNames);

As we can see in above example, we are collecting data in LIST.


String names[]={"Ram","Shyam","Mohan","BhanuPratap"};

Set<String> collectNames=Arrays.stream(names)
.collect(Collectors.toSet());

System.out.println(collectNames);

Here you can pass any Collector instance , we will have a walkthrough on Collector later.

collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)

Performs a mutable reduction operation on the elements of this stream.

This Collect(…) method has 3 args — supplier, accumulator and combiner

Supplier will supply the container.

Accumulator will accumulate the data

combiner combines the partial results when the reduction operation takes place in parallel.

Let’s consider below use-case:

ArrayList<String> namesMaster=new ArrayList<>();

ArrayList<String> names=new ArrayList<>();
names.add("Raj");
namesMaster.addAll(names);

names.add("Mohna");
namesMaster.addAll(names);

Lets understand below method:

collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)

Create one Supplier that supply ArrayList

Get Playwithjava’s stories in your inbox

Join Medium for free to get updates from this writer.

//Using Lambda Expression


Supplier<ArrayList<String>> getList=()->new ArrayList<>();

//Using Constructor Reference

Supplier<ArrayList<String>> getList1=ArrayList::new; //Constructor reference

Create accumulator of Type BiConsumer:

//Using Lambda Expression

BiConsumer<ArrayList,String> biConsumer= (existingList,newElement)->existingList.add(newElement);

//Using Method Reference

BiConsumer<ArrayList,String> biConsumer1=ArrayList::add; //Method Reference

Create Finisher of Type BiConsumer<List,List>:

//using Lambda Expression

BiConsumer<ArrayList<String>,ArrayList<String>> listBiConsumer1=(existingList,newList)->existingList.addAll(newList);

//using method Reference

BiConsumer<ArrayList<String>,ArrayList<String>> listBiConsumer2=ArrayList::addAll;

Use-case:

String userData[]={“Raj”,”Mahesh”,”Himesh”};

Collect these data as List:


List<String> stringArrayList1=Arrays.stream(userData)
.collect(()->new ArrayList<>(),

(existingList,newElement)->existingList.add(newElement),
(existingList,newList)->existingList.addAll(newList));

Using Method/Constructor reference:

List<String> stringArrayList=Arrays.stream(userData)
.collect(ArrayList::new,ArrayList::add,ArrayList::addAll);

--

--

Playwithjava
Playwithjava

Written by Playwithjava

I am a Software Techie having 10 yrs of experience in product development