A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/how-to-convert-a-stream-into-a-map-in-java/ below:

How to convert a Stream into a Map in Java

How to convert a Stream into a Map in Java

Last Updated : 15 Jul, 2025

Introduced in Java 8, the

Stream API

is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. In this article, the methods to convert a stream into a

map

is discussed.

Method 1: Using Collectors.toMap() Function

The

Collectors.toMap()

method takes two parameters as the input:

  1. KeyMapper: This function is used for extracting keys of the Map from stream value.
  2. ValueMapper: This function used for extracting the values of the map for the given key.

The following are the examples of the

toMap

function to convert the given stream into a map:

Method 2: Using Collectors

The

groupingBy

collector takes one function as input and creates a group of stream objects using that function. The following are the examples to convert a stream into a map using groupingBy collector.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4