Converting stream to collections and Arrays

Most programmers know about the intermediate operation and terminal operation. But only a few of them can decide what should we used to convert a stream in collection. In java stream collect method is used to java collection to list, java stream collect to array, java collector, java 8 collect.

Java stream collect

Here is the table content of the article will we will cover this topic.
1. How to Convert Stream to List, Map, and Set?
2. How to Convert Stream to Arrays?

Converting Stream to Collections: As you already know Stream is not a data structure, but collections are a data structure that is used to store the data. By use of the Collect() method, we can get List, Map or Set from the stream.

java stream collect

Converting Stream to List, Map, and Set

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExampleOfCollect
{
	public static void main(String[] args) 
	{
		List<String> names = new ArrayList<String>();
		names.add("A Java Website");
		names.add("B It learning a Website");
		names.add("C JavaGoal.com");
		names.add("D Java Website");
		
	
		
		List<String> filterList = names.stream().filter((a) -> a.endsWith("Website")).collect(Collectors.toList());
		System.out.println("Printing from List");
		filterList.forEach(System.out::println);
		
		Map<String, String> filteredMap = names.stream().filter((a) -> a.endsWith("Website")).collect(Collectors.toMap(Function.identity(), s -> s));
		System.out.println("Printing from Map");
		filteredMap.keySet().forEach(key -> System.out.println(filteredMap.get(key)));
		
		Set<String> filterSet = names.stream().filter((a) -> a.endsWith("Website")).collect(Collectors.toSet());
		System.out.println("Printing from Set");
		filterSet.forEach(System.out::println);
		
			
	}

}

Output: Printing from List
A Java Website
B It learning a Website
D Java Website
Printing from Map
B It learning a Website
D Java Website
A Java Website
Printing from Set
B It learning a Website
D Java Website
A Java Website

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ExampleOfCollectInUserDefined 
{
	public static void main(String[] args) 
	{
		ArrayList<Student> listOfStudent = new ArrayList<Student>();
		listOfStudent.add(new Student(101, "MCA", "Ram"));
		listOfStudent.add(new Student(102, "BCA", "Sham"));
		listOfStudent.add(new Student(103, "MSC", "Krishna"));
		listOfStudent.add(new Student(104, "MCA", "Satyam"));
		listOfStudent.add(new Student(105, "MCA", "Joshi"));
		
		//Converting Stream to List
		List<Student> filterList = listOfStudent.stream()
				.filter((student) -> student.getRollNo() > 101)
				.collect(Collectors.toList());
		
		System.out.println("Printing from filtered List of Student");
		filterList.forEach((stu) -> 
		{
			System.out.println("RollNo : "+stu.getRollNo());
			System.out.println("Class Name: "+stu.getClassName());
			System.out.println("Student Name : "+stu.getRollNo());
		});
		
		//Converting Stream to Map
		Map<Integer, Student> filteredMap = listOfStudent.stream()
				.filter((student) -> student.getClassName().equals("MCA"))
				.collect(Collectors.toMap(Student:: getRollNo , stuObj -> stuObj));
		System.out.println("Printing from filtered Map data of Student");
		filteredMap.values().forEach(stu -> 
		{
			System.out.println("RollNo : "+stu.getRollNo());
			System.out.println("Class Name: "+stu.getClassName());
			System.out.println("Student Name : "+stu.getRollNo());
		});
		
		//Converting Stream to Set
		Set<Student> filterSet = listOfStudent.stream()
				.filter(student -> student.getRollNo() < 103)
				.collect(Collectors.toSet());
		System.out.println("Printing from filterd Set data of Student");
		filterSet.forEach((stu) -> 
		{
			System.out.println("RollNo : "+stu.getRollNo());
			System.out.println("Class Name: "+stu.getClassName());
			System.out.println("Student Name : "+stu.getRollNo());
		});	
	}
}

class Student 
{
	int rollNo;
	String className;
	String name;
	
	public Student(int rollNo, String className, String name) 
	{
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
	}
	public int getRollNo() 
	{
		return rollNo;
	}
	public void setRollNo(int rollNo) 
	{
		this.rollNo = rollNo;
	}
	public String getClassName() 
	{
		return className;
	}
	public void setClassName(String className) 
	{
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public boolean equals(Object obj) 
	{
		return this.rollNo == ((Student)obj).getRollNo();
	}
	@Override
	public int hashCode() 
	{
		return this.rollNo;
	}
}

Output: Printing from filtered List of Student
RollNo : 102
Class Name: BCA
Student Name : 102
RollNo : 103
Class Name: MSC
Student Name : 103
RollNo : 104
Class Name: MCA
Student Name : 104
RollNo : 105
Class Name: MCA
Student Name : 105
Printing from filtered Map data of Student
RollNo : 101
Class Name: MCA
Student Name : 101
RollNo : 104
Class Name: MCA
Student Name : 104
RollNo : 105
Class Name: MCA
Student Name : 105
Printing from filterd Set data of Student
RollNo : 101
Class Name: MCA
Student Name : 101
RollNo : 102
Class Name: BCA
Student Name : 102

Converting Stream to Arrays

The toArray() method declared in Stream interface. The toArray() returns an array of elements from the stream. It is a terminal operation of Stream. As you already know Stream is not a data structure, but the Arrays is a data structure that is used to store the data.

import java.util.ArrayList;
import java.util.List;

public class ExampleOfCollect
{
	public static void main(String[] args) 
	{
		
		List<String> names = new ArrayList<String>();
		names.add("RAM");
		names.add("Ravi");
		names.add("Sham");
		names.add("Shiv");
		names.add("Raman");
		
		String[] filteredlist = names.stream()
				.filter(s -> s.startsWith("R"))
				.toArray(String[] :: new);
		for (String stu : filteredlist) 
		{
			System.out.println("Name of Student:"+ stu);
		}
	}
}

Output: Name of Student:RAM
Name of Student:Ravi
Name of Student:Raman

import java.util.ArrayList;

public class ExampleOfCollectInUserDefined 
{
	public static void main(String[] args) 
	{
		ArrayList<Book> listOfStudent = new ArrayList<Book>();
		listOfStudent.add(new Book(1011, "Digital", "RRK"));
		listOfStudent.add(new Book(1012, "Mathemetics", "LP"));
		listOfStudent.add(new Book(1033, "LOC", "PO"));
		listOfStudent.add(new Book(1048, "PC", "SK"));
		listOfStudent.add(new Book(1050, "Fundamental", "NMM"));
		
		//Converting Stream to Array
		Book[] filteredlist = listOfStudent.stream()
				.filter(book -> book.getBookId() > 1011)
				.toArray(Book[] :: new);
		for (Book book : filteredlist) 
		{
			System.out.println("Id of Book:"+ book.getBookId());
			System.out.println("Name of Book:"+ book.getName());
			System.out.println("Author of Book:"+ book.getAuthorName());
		}	
	}
}

class Book 
{
	int bookId;
	String name;
	String authorName;
	public Book(int bookId, String name, String authorName) 
	{
		this.bookId = bookId;
		this.name = name;
		this.authorName = authorName;
	}
	public int getBookId() 
	{
		return bookId;
	}
	public void setBookId(int bookId) 
	{
		this.bookId = bookId;
	}
	public String getName() 
	{
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthorName() {
		return authorName;
	}
	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}
}

Output: Id of Book:1012
Name of Book:Mathemetics
Author of Book:LP
Id of Book:1033
Name of Book:LOC
Author of Book:PO
Id of Book:1048
Name of Book:PC
Author of Book:SK
Id of Book:1050
Name of Book:Fundamental
Author of Book:NMM

Leave a Comment