collect() Method Example In Java

The stream.collect() method accumulate elements of any Stream into a Collection, it is a part of stream terminal operation.
The Collector class provides different methods like toList(), toSet(), toMap(), and toConcurrentMap() to collect the result of Stream into List, Set, Map, and ConcurrentMap in Java. It accepts a Collector in arguments to accumulate elements of Stream into a specified Collection such as stream().collect( Collectors.toList() ), stream().collect( Collectors.toSet() ), stream().collect (Collectors.toMap(....) ), stream().collect( Collectors.counting()) etc..

It is used to perform different types of reduction operations such as calculating the sum of numeric values, finding the minimum or maximum number, concatenating strings to new string, collecting elements into a new stream.

  1. The stream.collect() method is a part of stream terminal operation.
  2. The stream.collect() method accumulate elements of any Stream into a Collection.
  3. It is used to convert to list using, stream().collect(Collectors.toList()).
  4. It is used to convert to map using, stream().collect(Collectors.toMap()).
  5. It is used to convert to set using, stream().collect(Collectors.toSet()).
  6. It is used for calculating the sum of numeric values.
  7. It is used for collecting elements into a new stream.
  8. It is used for concatenating strings to new string.
  9. It is used for finding the minimum or maximum number.

Syntax:

 stream.collect() 

Example With Syntax:

List<Integer> evenNumList = list.stream().filter( i-> i%2 == 0 )
    //Converting filtered data to a List using collect method
    .collect(Collectors.toList());

Example of collect() method for the convrting Stream to List

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class CollectMethodExample {
 public static void main(String args[]){
    //Declaring List
    List<Integer> intList = new ArrayList< Integer >();
    intList.add( 6 );
    intList.add( 11 );
    intList.add( 14 );
    intList.add( 7 );
    intList.add( 3 );
    intList.add( 2 );
    intList.add( 16 );
    intList.add( 15 );
    intList.add( 19 );

    List<Integer> evenNumList = 
        intList.stream()
        .filter( i-> i%2==0 )
        //Converting filtered data to a List
        .collect( Collectors.toList() );

    System.out.println( "Even Num List" +evenNumList )

  }
}

Output:
    Even Num List [6, 14, 2, 16]

Example of collect() method for the convrting Stream to Map

public class Students {
Integer rollNumber;
 final String studentName;;
 Students( Integer rollNumber,String name){
   this.rollNumber=rollNumber;
   this.studentName=name;
 }

 public String toString() {
    return this.rollNumber + ", "+ this.studentName;
 }
}


//Importing required classes
import java.util.*;
import java.io.*;
import java.util.stream.*;

public class CollectMapExample {
  public static void main (String args[]){
  List<Students> studentsList = new ArrayList< Students >();
  studentsList.add( new Students( 1, "Pankaj" ));
  studentsList.add( new Students( 6, "Prakash" ));
  studentsList.add( new Students( 3, "Karim" ));
  studentsList.add( new Students( 2, "Shyam" ));
  studentsList.add( new Students( 4, "Tejas" ));
  studentsList.add( new Students( 5, "Rahul" ));
               
  Map<Integer, String> studentMap =
        studentsList.stream()
        .collect(Collectors
        .toMap(
            p -> p.rollNumber, p -> p.studentName));

 //Printing the map
  System.out.println( "Student Map " +studentMap);
  
  }        
}

Output:
 {1=Pankaj, 2=Shyam, 3=Karim, 4=Tejas, 5=Rahul, 6=Prakash}